private void SetFileNameToSelectedItem(IntPtr hListView, IntPtr hFNCombo, int selectedIndex)
        {
            if (selectedIndex >= 0)
            {
                var lvitem = new InteropUtil.LVITEM();
                lvitem.mask = InteropUtil.LVIF_TEXT;
                IntPtr nativeBuffer = Marshal.AllocCoTaskMem(InteropUtil.NumberOfFileChars * 2);
                for (int i = 0; i < InteropUtil.NumberOfFileChars; ++i)
                {
                    Marshal.WriteInt16(nativeBuffer, i * 2, '\0');
                }
                string name;

                try
                {
                    Marshal.WriteInt16(nativeBuffer, 0);
                    lvitem.pszText    = nativeBuffer;
                    lvitem.cchTextMax = InteropUtil.NumberOfFileChars;
                    uint length = InteropUtil.SendListViewMessage(hListView, InteropUtil.LVM_GETITEMTEXT,
                                                                  (uint)selectedIndex, ref lvitem);
                    name = Marshal.PtrToStringUni(lvitem.pszText, (int)length);
                }
                finally
                {
                    Marshal.FreeCoTaskMem(nativeBuffer);
                }

                if (name != null && m_currentFolder != null)
                {
                    try
                    {
                        string path = System.IO.Path.Combine(m_currentFolder, name);
                        if (Directory.Exists(path))
                        {
                            InteropUtil.SetWindowTextW(hFNCombo, name);
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
            }
        }
        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);
        }
        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);
        }
Beispiel #4
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);
        }