private int ProcessNotifyMessage(IntPtr hWnd, InteropUtil.OFNOTIFY notifyData) { switch (notifyData.hdr_code) { case InteropUtil.CDN_FOLDERCHANGE: { string newFolder = GetTextFromCommonDialog(hWnd.GetParent().AssumeNonZero(), InteropUtil.CDM_GETFOLDERPATH); if (m_currentFolder != null && newFolder != null && newFolder.PathContains(m_currentFolder)) { m_suppressSelectionChange = true; } m_currentFolder = newFolder; IntPtr fileNameCombo = hWnd.GetParent().AssumeNonZero().GetDlgItem(InteropUtil.ID_FileNameCombo).AssumeNonZero(); if (m_hasDirChangeFired) { fileNameCombo.SetWindowTextW(""); } m_hasDirChangeFired = true; break; } case InteropUtil.CDN_FILEOK: { if (!AcceptFiles) { return(1); } break; } case InteropUtil.CDN_INITDONE: { IntPtr hParent = hWnd.GetParent(); IntPtr hFile = hParent.AssumeNonZero().GetDlgItem(InteropUtil.ID_FileNameCombo).AssumeNonZero(); hFile.SetFocus(); break; } } return(0); }
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: { InteropUtil.OFNOTIFY notifyData = (InteropUtil.OFNOTIFY)Marshal.PtrToStructure(lparam, typeof(InteropUtil.OFNOTIFY)); int results = ProcessNotifyMessage(hWnd, notifyData); if (results != 0) { hWnd.SetWindowLongW(InteropUtil.DWL_MSGRESULT, results); return((IntPtr)results); } break; } case InteropUtil.WM_SIZE: { ResizeCustomControl(hWnd); break; } case InteropUtil.WM_COMMAND: { unchecked { IntPtr hParent = hWnd.GetParent().AssumeNonZero(); 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. hParent.SendMessage(InteropUtil.WM_CLOSE, 0, 0); break; } case InteropUtil.ID_SELECT: { IntPtr hFileName = hParent.GetDlgItem(InteropUtil.ID_FileNameCombo); string currentText = (hFileName.GetWindowTextW() ?? "").Trim(); if (currentText == "" && !string.IsNullOrEmpty(m_currentFolder)) { //there's not text in the box, so the user must want to select the current folder. m_useCurrentDir = true; hParent.SendMessage(InteropUtil.WM_CLOSE, 0, 0); break; } else if (System.IO.Path.IsPathRooted(currentText)) { if (Directory.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. m_useCurrentDir = true; m_currentFolder = currentText; hParent.SendMessage(InteropUtil.WM_CLOSE, 0, 0); break; } } else if (!string.IsNullOrEmpty(m_currentFolder) && currentText != "") { string combined = System.IO.Path.Combine(m_currentFolder, currentText); if (Directory.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. m_useCurrentDir = true; m_currentFolder = combined; hParent.SendMessage(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. hParent.SendMessage ( InteropUtil.WM_COMMAND, (InteropUtil.BN_CLICKED << 16) | InteropUtil.IDOK, unchecked ((uint)hParent.GetDlgItem(InteropUtil.IDOK)) ); break; } } } } break; } } return(base.HookProc(hWnd, msg, wParam, lparam)); }