private bool AddSelectedItemsFromSelection(IntPtr listview)
        {
            uint count = InteropUtil.SendMessage(listview,
                                                 InteropUtil.LVM_GETSELECTEDCOUNT, 0, 0);

            //if there is at least one item selected we always return these ones
            if (count > 0)
            {
                int ind = -1;
                for (int i = 0; i < count; i++)
                {
                    ind = InteropUtil.SendMessageInt(listview, InteropUtil.LVM_GETNEXTITEM,
                                                     ind,
                                                     InteropUtil.LVNI_SELECTED);
                    //with the index we can get the item's text
                    string item = GetFileNameFromSelectedItem(listview, ind);
                    _selected.Add(item);
                }
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        private string GetTextFromCommonDialog(IntPtr hWnd, uint msg)
        {
            string str    = null;
            var    buffer = Marshal.AllocCoTaskMem(2 * InteropUtil.NumberOfFileChars);

            try
            {
                InteropUtil.SendMessage(hWnd, msg, InteropUtil.NumberOfFileChars, unchecked ((uint)buffer));
                var chars = new char[InteropUtil.NumberOfFileChars];
                Marshal.Copy(buffer, chars, 0, chars.Length);
                var firstZeroTerm = ((IList)chars).IndexOf('\0');

                if (firstZeroTerm >= 0 && firstZeroTerm <= chars.Length - 1)
                {
                    str = new string(chars, 0, firstZeroTerm);
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
            }
            return(str);
        }
        private void InitDialog(IntPtr hWnd)
        {
            m_hWnd = hWnd;

            IntPtr hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd));

            InteropUtil.SetWindowSubclass(hParent, m_openFileSubClassDelegate, 0, 0);

            //disable and hide the filter combo box
            IntPtr hFilterCombo = InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hParent, InteropUtil.ID_FilterCombo));

            InteropUtil.EnableWindow(hFilterCombo, false);
            InteropUtil.SendMessage(hParent, InteropUtil.CDM_HIDECONTROL, InteropUtil.ID_FilterCombo, 0);
            InteropUtil.SendMessage(hParent, InteropUtil.CDM_HIDECONTROL, InteropUtil.ID_FilterLabel, 0);

            //update the file name label
            IntPtr hFileNameLabel =
                InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hParent, InteropUtil.ID_FileNameLabel));

            if (FileNameLabel != String.Empty)
            {
                InteropUtil.SendMessageString(hFileNameLabel, InteropUtil.WM_SETTEXT, 0, FileNameLabel);
            }

            //find the button controls in the parent
            IntPtr hOkButton     = InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hParent, InteropUtil.IDOK));
            IntPtr hCancelButton = InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hParent, InteropUtil.IDCANCEL));

            //We don't want the accelerator keys for the ok and cancel buttons to work, because
            //they are not shown on the dialog. However, we still want the buttons enabled
            //so that "esc" and "enter" have the behavior they used to. So, we just
            //clear out their text instead.
            InteropUtil.SetWindowTextW(hOkButton, String.Empty);
            InteropUtil.SetWindowTextW(hCancelButton, String.Empty);

            //find our button controls
            IntPtr hSelectButton       = InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hWnd, InteropUtil.ID_SELECT));
            IntPtr hCustomCancelButton =
                InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hWnd, InteropUtil.ID_CUSTOM_CANCEL));

            if (!String.IsNullOrEmpty(SelectLabel))
            {
                InteropUtil.SetWindowTextW(hSelectButton, SelectLabel);
            }
            if (!String.IsNullOrEmpty(CancelLabel))
            {
                InteropUtil.SetWindowTextW(hCustomCancelButton, CancelLabel);
            }

            //copy the font from the parent's buttons
            InteropUtil.LoadFontFrom(hSelectButton, hOkButton);
            InteropUtil.LoadFontFrom(hCustomCancelButton, hCancelButton);

            InteropUtil.WINDOWPLACEMENT cancelLoc = InteropUtil.GetWindowPlacement(hCancelButton);

            //hide the ok and cancel buttons
            InteropUtil.SendMessage(hParent, InteropUtil.CDM_HIDECONTROL, InteropUtil.IDOK, 0);
            InteropUtil.SendMessage(hParent, InteropUtil.CDM_HIDECONTROL, InteropUtil.IDCANCEL, 0);

            //expand the file name combo to take up the space left by the OK and cancel buttons.
            IntPtr hFileName = InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hParent, InteropUtil.ID_FileNameCombo));

            InteropUtil.WINDOWPLACEMENT fileNameLoc = InteropUtil.GetWindowPlacement(hFileName);
            fileNameLoc.Right = InteropUtil.GetWindowPlacement(hOkButton).Right;
            InteropUtil.SetWindowPlacement(hFileName, ref fileNameLoc);

            InteropUtil.WINDOWPLACEMENT parentLoc = InteropUtil.GetWindowPlacement(hParent);

            //subtract the height of the missing cancel button
            parentLoc.Bottom -= (cancelLoc.Bottom - cancelLoc.Top);
            InteropUtil.SetWindowPlacement(hParent, ref parentLoc);

            //move the select and custom cancel buttons to the right hand side of the window:

            InteropUtil.WINDOWPLACEMENT selectLoc       = InteropUtil.GetWindowPlacement(hSelectButton);
            InteropUtil.WINDOWPLACEMENT customCancelLoc = InteropUtil.GetWindowPlacement(hCustomCancelButton);
            m_cancelWidth = customCancelLoc.Right - customCancelLoc.Left;
            m_selectWidth = selectLoc.Right - selectLoc.Left;
            m_buttonGap   = customCancelLoc.Left - selectLoc.Right;

            InteropUtil.WINDOWPLACEMENT ctrlLoc = InteropUtil.GetWindowPlacement(hWnd);
            ctrlLoc.Right = fileNameLoc.Right;

            //ResizeCustomControl(hWnd, fileNameLoc.Right, hCustomCancelButton, hSelectButton);
            ResizeCustomControl(hWnd);
        }
        /// <summary>
        /// Defines the common dialog box hook procedure that is overridden to add specific functionality to a common dialog box.
        /// </summary>
        /// <returns>
        /// A zero value if the default dialog box procedure processes the message; a nonzero value if the default dialog box procedure ignores the message.
        /// </returns>
        /// <param name="hWnd">The handle to the dialog box window. </param><param name="msg">The message being received. </param><param name="wparam">Additional information about the message. </param><param name="lparam">Additional information about the message. </param>
        protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lparam)
        {
            switch (unchecked ((uint)msg))
            {
            case InteropUtil.WM_INITDIALOG:
            {
                InitDialog(hWnd);
                break;
            }

            case InteropUtil.WM_NOTIFY:
            {
                var notifyData =
                    (InteropUtil.OFNOTIFY)Marshal.PtrToStructure(lparam, typeof(InteropUtil.OFNOTIFY));
                int results = ProcessNotifyMessage(hWnd, notifyData);
                if (results != 0)
                {
                    InteropUtil.SetWindowLongW(hWnd, InteropUtil.DWL_MSGRESULT, results);
                    return((IntPtr)results);
                }
                break;
            }

            case InteropUtil.WM_SIZE:
            {
                ResizeCustomControl(hWnd);
                break;
            }

            case (InteropUtil.BN_CLICKED << 16) | InteropUtil.IDOK:
            {
                break;
            }

            case InteropUtil.WM_COMMAND:
            {
                unchecked
                {
                    IntPtr hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd));
                    uint   code    = HIGH((uint)wParam);
                    uint   id      = LOW((uint)wParam);
                    if (code == InteropUtil.BN_CLICKED)
                    {
                        switch (id)
                        {
                        case InteropUtil.ID_CUSTOM_CANCEL:
                        {
                            //The user clicked our custom cancel button. Close the dialog.
                            InteropUtil.SendMessage(hParent, InteropUtil.WM_CLOSE, 0, 0);
                            break;
                        }

                        case InteropUtil.ID_SELECT:
                        {
                            if (ProcessSelection(hParent, true))
                            {
                                InteropUtil.SendMessage(hParent, InteropUtil.WM_CLOSE, 0, 0);
                                break;
                            }
                            //The user has not selected an existing folder.
                            //So we translate a click of our "Select" button into the OK button and forward the request to the
                            //open file dialog.
                            InteropUtil.SendMessage
                                (hParent,
                                InteropUtil.WM_COMMAND,
                                (InteropUtil.BN_CLICKED << 16) | InteropUtil.IDOK,
                                unchecked ((uint)InteropUtil.GetDlgItem(hParent, InteropUtil.IDOK))
                                );


                            break;
                        }
                        }
                    }
                }
                break;
            }
            }
            return(base.HookProc(hWnd, msg, wParam, lparam));
        }
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            InteropUtil.Assume(Marshal.SystemDefaultCharSize == 2, "The character size should be 2");

            IntPtr nativeBuffer = Marshal.AllocCoTaskMem(InteropUtil.NumberOfFileChars * 2);
            IntPtr filterBuffer = IntPtr.Zero;

            _selected = new List <string>();

            try
            {
                var openFileName = new InteropUtil.OpenFileName();
                openFileName.Initialize();
                openFileName.hwndOwner = hwndOwner;

                var chars = new char[InteropUtil.NumberOfFileChars];

                try
                {
                    if (File.Exists(Path))
                    {
                        if (AcceptFiles)
                        {
                            string fileName = System.IO.Path.GetFileName(Path);
                            int    length   = Math.Min(fileName.Length, InteropUtil.NumberOfFileChars);
                            fileName.CopyTo(0, chars, 0, length);
                            openFileName.lpstrInitialDir = System.IO.Path.GetDirectoryName(Path);
                        }
                        else
                        {
                            openFileName.lpstrInitialDir = System.IO.Path.GetDirectoryName(Path);
                        }
                    }
                    else if (Directory.Exists(Path))
                    {
                        openFileName.lpstrInitialDir = Path;
                    }
                    else
                    {
                        //the path does not exist.
                        //We don't just want to throw it away, however.
                        //The initial path we get is most likely provided by the user in some way.
                        //It could be what they typed into a text box before clicking a browse button,
                        //or it could be a value they had entered previously that used to be valid, but now no longer exists.
                        //In any case, we don't want to throw out the user's text. So, we find the first parent
                        //directory of Path that exists on disk.
                        //We will set the initial directory to that path, and then set the initial file to
                        //the rest of the path. The user will get an error if the click "OK"m saying that the selected path
                        //doesn't exist, but that's ok. If we didn't do this, and showed the path, if they clicked
                        //OK without making changes we would actually change the selected path, which would be bad.
                        //This way, if the users want's to change the folder, he actually has to change something.
                        string pathToShow;
                        InitializePathDNE(Path, out openFileName.lpstrInitialDir, out pathToShow);
                        pathToShow = pathToShow ?? String.Empty;
                        int length = Math.Min(pathToShow.Length, InteropUtil.NumberOfFileChars);
                        pathToShow.CopyTo(0, chars, 0, length);
                    }
                }
                catch
                {
                }

                Marshal.Copy(chars, 0, nativeBuffer, chars.Length);

                openFileName.lpstrFile = nativeBuffer;

                if (!AcceptFiles)
                {
                    string str = string.Format("Folders\0*.{0}-{1}\0\0", Guid.NewGuid().ToString("N"),
                                               Guid.NewGuid().ToString("N"));
                    filterBuffer = openFileName.lpstrFilter = Marshal.StringToCoTaskMemUni(str);
                }
                else
                {
                    openFileName.lpstrFilter = IntPtr.Zero;
                }

                openFileName.nMaxCustFilter = 0;
                openFileName.nFilterIndex   = 0;
                openFileName.nMaxFile       = InteropUtil.NumberOfFileChars;
                openFileName.nMaxFileTitle  = 0;
                openFileName.lpstrTitle     = Title;
                openFileName.lpfnHook       = m_hookDelegate;
                openFileName.templateID     = InteropUtil.IDD_CustomOpenDialog;
                openFileName.hInstance      = Marshal.GetHINSTANCE(typeof(SelectFileAndFolderDialog).Module);
                openFileName.Flags          =
                    InteropUtil.OFN_DONTADDTORECENT |
                    InteropUtil.OFN_ENABLEHOOK |
                    InteropUtil.OFN_ENABLESIZING |
                    InteropUtil.OFN_NOTESTFILECREATE |
                    InteropUtil.OFN_ALLOWMULTISELECT |
                    InteropUtil.OFN_EXPLORER |
                    InteropUtil.OFN_FILEMUSTEXIST |
                    InteropUtil.OFN_PATHMUSTEXIST |
                    InteropUtil.OFN_NODEREFERENCELINKS |
                    InteropUtil.OFN_ENABLETEMPLATE |
                    (ShowReadOnly ? 0 : InteropUtil.OFN_HIDEREADONLY);

                m_useCurrentDir = false;
                bool hideFileExtSettingChanged = false;

                try
                {
                    if (GetHideFileExtensionSetting())
                    {
                        HideFileExtension(false);
                        hideFileExtSettingChanged = true;
                    }

                    bool ret = false;
                    try
                    {
                        ret = InteropUtil.GetOpenFileNameW(ref openFileName);
                    }
                    catch (Exception e)
                    {
                        IntPtr hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(m_hWnd));
                        InteropUtil.SendMessage(hParent, InteropUtil.WM_CLOSE, 0, 0);
                        throw e;
                    }
                    //var extErrpr = InteropUtil.CommDlgExtendedError();
                    //InteropUtil.CheckForWin32Error();

                    if (m_useCurrentDir)
                    {
                        Path = m_currentFolder;
                        return(true);
                    }
                    else if (ret)
                    {
                        Marshal.Copy(nativeBuffer, chars, 0, chars.Length);
                        int firstZeroTerm = ((IList)chars).IndexOf('\0');
                        if (firstZeroTerm >= 0 && firstZeroTerm <= chars.Length - 1)
                        {
                            Path = new string(chars, 0, firstZeroTerm);
                        }
                    }
                    return(ret);
                }
                finally
                {
                    //revert registry setting
                    if (hideFileExtSettingChanged)
                    {
                        HideFileExtension(true);
                    }
                }
            }
            finally
            {
                Marshal.FreeCoTaskMem(nativeBuffer);
                if (filterBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(filterBuffer);
                }
            }
        }