private void ResizeCustomControl(IntPtr hWnd)
        {
            if (hWnd == m_hWnd)
            {
                IntPtr hSelectButton =
                    InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(InteropUtil.AssumeNonZero(hWnd),
                                                                     InteropUtil.ID_SELECT));
                IntPtr hOkButton =
                    InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(InteropUtil.AssumeNonZero(hWnd),
                                                                     InteropUtil.ID_CUSTOM_CANCEL));

                IntPtr hParent  = InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd));
                IntPtr fileName =
                    InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(hParent, InteropUtil.ID_FileNameCombo));

                /*var right = fileName.GetWindowPlacement().Right;
                 * var top = hSelectButton.GetWindowPlacement().Top;*/

                var rect = new InteropUtil.RECT();
                InteropUtil.WINDOWPLACEMENT selectRect = InteropUtil.GetWindowPlacement(hSelectButton);

                rect.top    = selectRect.Top;
                rect.bottom = selectRect.Bottom;
                rect.right  = InteropUtil.GetWindowPlacement(fileName).Right;
                rect.left   = rect.right - (m_cancelWidth + m_buttonGap + m_selectWidth);

                ResizeCustomControl(hWnd, rect, hOkButton, hSelectButton);
            }
        }
        private bool ProcessSelection(IntPtr handle, bool preferSelection)
        {
            IntPtr shelldll_defview = InteropUtil.GetDlgItem(handle,
                                                             InteropUtil.ID_FileList);
            IntPtr listview = InteropUtil.GetDlgItem(shelldll_defview, 1);
            IntPtr focus    = InteropUtil.GetFocus();

            if (listview == focus || preferSelection)
            {
                return(AddSelectedItemsFromSelection(listview));
            }
            else
            {
                //check the content of the file combobox
                IntPtr hFileName = InteropUtil.GetDlgItem(handle,
                                                          InteropUtil.ID_FileNameCombo);
                string currentText = (InteropUtil.GetWindowTextW(hFileName) ?? "").Trim();
                if (!String.IsNullOrEmpty(currentText))
                {
                    try
                    {
                        if (System.IO.Path.IsPathRooted(currentText))
                        {
                            if (Directory.Exists(currentText) || File.Exists(currentText))
                            {
                                //the contents of the text box are a rooted path, that points to an existing directory.
                                //we interpret that to mean that the user wants to select that directory.
                                _selected.Add(currentText);
                                return(true);
                            }
                        }
                        else if (!String.IsNullOrEmpty(m_currentFolder))
                        {
                            string combined = System.IO.Path.Combine(m_currentFolder,
                                                                     currentText);
                            if (Directory.Exists(combined) || File.Exists(combined))
                            {
                                //the contents of the text box are a relative path, that points to a
                                //an existing directory. We interpret the users intent to mean that they wanted
                                //to select the existing path.
                                _selected.Add(combined);
                                return(true);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //try to add the selection
                        if (AddSelectedItemsFromSelection(listview))
                        {
                            return(true);
                        }
                    }
                }
                //forward all wrong inputs to the standard mechanism
                return(false);
            }
        }
        private int DefViewSubClass
        (
            IntPtr hWnd,
            uint uMsg,
            IntPtr wParam,
            IntPtr lParam,
            IntPtr uIdSubclass,
            uint dwRefData
        )
        {
            if (uMsg == InteropUtil.WM_NOTIFY)
            {
                var header = (InteropUtil.NMHDR)Marshal.PtrToStructure(lParam, typeof(InteropUtil.NMHDR));
                if (header.code == InteropUtil.LVN_ITEMCHANGED && header.hwndFrom != IntPtr.Zero && header.idFrom == 1)
                {
                    var nmListView =
                        (InteropUtil.NMLISTVIEW)Marshal.PtrToStructure(lParam, typeof(InteropUtil.NMLISTVIEW));
                    bool oldSelected = (nmListView.uOldState & InteropUtil.LVIS_SELECTED) != 0;
                    bool newSelected = (nmListView.uNewState & InteropUtil.LVIS_SELECTED) != 0;
                    if (!oldSelected && newSelected)
                    {
                        if (!m_suppressSelectionChange)
                        {
                            //the item went from not selected to being selected
                            //so we want to look and see if the selected item is a folder, and if so
                            //change the text of the item box to be the item on the folder. But, before we do that
                            //we want to make sure that the box isn't currently focused.
                            IntPtr hParent    = InteropUtil.GetParent(hWnd);
                            IntPtr hFNCombo   = InteropUtil.GetDlgItem(hParent, InteropUtil.ID_FileNameCombo);
                            IntPtr hFNEditBox = InteropUtil.GetDlgItem(hParent, InteropUtil.ID_FileNameTextBox);
                            IntPtr hFocus     = InteropUtil.GetFocus();

                            if
                            (
                                (hFNCombo == IntPtr.Zero || hFNCombo != hFocus) &&
                                (hFNEditBox == IntPtr.Zero || hFNEditBox != hFocus)
                            )
                            {
                                SetFileNameToSelectedItem(header.hwndFrom, hFNCombo, nmListView.iItem);
                            }
                        }
                        m_suppressSelectionChange = false;
                    }
                }
            }
            return(InteropUtil.DefSubclassProc(hWnd, uMsg, wParam, lParam));
        }
        private int ProcessNotifyMessage(IntPtr hWnd, InteropUtil.OFNOTIFY notifyData)
        {
            switch (notifyData.hdr_code)
            {
            case InteropUtil.CDN_FOLDERCHANGE:
            {
                //CDM_GETFOLDERPATH returns garbage for some standard folders like 'Libraries'
                //var newFolder = GetTextFromCommonDialog(InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd)),
                //                  InteropUtil.CDM_GETFOLDERPATH);
                string newFolder =
                    GetTextFromCommonDialog(InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd)),
                                            InteropUtil.CDM_GETFILEPATH);

                /*
                 * if (m_currentFolder != null && newFolder != null &&
                 *  Util.PathContains(newFolder, m_currentFolder))
                 * {
                 *  m_suppressSelectionChange = true;
                 * }
                 */

                m_currentFolder = newFolder;

                /* #5841 On Windows XP when changing the folder 'newFolder' contains the selected directory twice (e.g. c:\temp\i386\i386)
                 * HACK
                 */
                if (!Directory.Exists(newFolder))
                {
                    try
                    {
                        String lastPart   = System.IO.Path.GetFileName(newFolder);
                        String parent     = Directory.GetParent(newFolder).FullName;
                        String parentFile = System.IO.Path.GetFileName(parent);
                        if (lastPart.Equals(parentFile))
                        {
                            m_currentFolder = parent;
                        }
                    }
                    catch (Exception)
                    {
                        //ignore
                    }
                }
                IntPtr fileNameCombo =
                    InteropUtil.AssumeNonZero(
                        InteropUtil.GetDlgItem(InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd)),
                                               InteropUtil.ID_FileNameCombo));
                if (m_hasDirChangeFired)
                {
                    InteropUtil.SetWindowTextW(fileNameCombo, String.Empty);
                }
                m_hasDirChangeFired = true;

                //refresh the file list to make sure that the extension is shown properly
                IntPtr hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd));
                SetForegroundWindow(hParent);
                SendKeys.SendWait("{F5}");

                break;
            }

            case InteropUtil.CDN_FILEOK:
            {
                if (!AcceptFiles)
                {
                    return(1);
                }

                IntPtr hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd));
                ProcessSelection(hParent, false);

                break;
            }

            case InteropUtil.CDN_INITDONE:
            {
                IntPtr hParent = InteropUtil.GetParent(hWnd);
                IntPtr hFile   =
                    InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(InteropUtil.AssumeNonZero(hParent),
                                                                     InteropUtil.ID_FileNameCombo));
                InteropUtil.SetFocus(hFile);
                break;
            }
            }
            return(0);
        }
        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));
        }
예제 #7
0
        private int ProcessNotifyMessage(IntPtr hWnd, InteropUtil.OFNOTIFY notifyData)
        {
            switch (notifyData.hdr_code)
            {
            case InteropUtil.CDN_FOLDERCHANGE:
            {
                //CDM_GETFOLDERPATH returns garbage for some standard folders like 'Libraries'
                //var newFolder = GetTextFromCommonDialog(InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd)),
                //                  InteropUtil.CDM_GETFOLDERPATH);
                var newFolder = GetTextFromCommonDialog(InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd)),
                                                        InteropUtil.CDM_GETFILEPATH);

                /*
                 * if (m_currentFolder != null && newFolder != null &&
                 *  Util.PathContains(newFolder, m_currentFolder))
                 * {
                 *  m_suppressSelectionChange = true;
                 * }
                 */

                m_currentFolder = newFolder;
                var fileNameCombo =
                    InteropUtil.AssumeNonZero(
                        InteropUtil.GetDlgItem(InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd)),
                                               InteropUtil.ID_FileNameCombo));
                if (m_hasDirChangeFired)
                {
                    InteropUtil.SetWindowTextW(fileNameCombo, "");
                }
                m_hasDirChangeFired = true;

                //refresh the file list to make sure that the extension is shown properly
                var hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd));
                SetForegroundWindow(hParent);
                SendKeys.SendWait("{F5}");

                break;
            }

            case InteropUtil.CDN_FILEOK:
            {
                if (!AcceptFiles)
                {
                    return(1);
                }

                var hParent = InteropUtil.AssumeNonZero(InteropUtil.GetParent(hWnd));
                ProcessSelection(hParent, false);

                break;
            }

            case InteropUtil.CDN_INITDONE:
            {
                var hParent = InteropUtil.GetParent(hWnd);
                var hFile   =
                    InteropUtil.AssumeNonZero(InteropUtil.GetDlgItem(InteropUtil.AssumeNonZero(hParent),
                                                                     InteropUtil.ID_FileNameCombo));
                InteropUtil.SetFocus(hFile);
                break;
            }
            }
            return(0);
        }