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)); var 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 { var hParent = hWnd.GetParent().AssumeNonZero(); var code = HIGH((uint)wParam); var 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: { var hFileName = hParent.GetDlgItem(InteropUtil.ID_FileNameCombo); var 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 != "") { var 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); }