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)); }