protected override void OnSelectionChanged() { base.OnSelectionChanged(); if (m_selectedPidls == null || m_selectedPidls.Length == 0) { if (okButton.Text != "&Save") { okButton.Text = "&Save"; } if (!fileNameComboBox.Focused && fileNameComboBox.Text.Trim() == string.Empty) { m_ignoreFileNameChange = true; fileNameComboBox.Text = FileName; } } else { IntPtr[] pidls = new IntPtr[] { m_selectedPidls[0] }; NativeMethods.SHGFAO attribs = NativeMethods.SHGFAO.SFGAO_STREAM; m_currentFolder.GetAttributesOf(1, pidls, ref attribs); // If a folder is selected set ok button text to Open, otherwise Save if ((attribs & NativeMethods.SHGFAO.SFGAO_STREAM) == 0) { if (okButton.Text != "&Open") { okButton.Text = "&Open"; } } else { if (okButton.Text != "&Save") { okButton.Text = "&Save"; } m_ignoreFileNameChange = true; fileNameComboBox.Text = GetDisplayName(m_currentFolder, m_selectedPidls[0]); } } okButton.Enabled = ((m_selectedPidls != null && m_selectedPidls.Length > 0) || !string.IsNullOrEmpty(fileNameComboBox.Text)); AcceptButton = okButton.Enabled ? okButton : cancelButton; }
internal static int GetIconIndex(IntPtr pidl, bool selectedIcon, bool openIcon) { NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(pidl, 0, ref info, NativeMethods.cbFileInfo, NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_SYSICONINDEX); int index = info.iIcon; bool hasOverlay = false; // true if it's an overlay int rVal; // The returned Index IntPtr[] pidls = new IntPtr[] { pidl }; NativeMethods.SHGFAO attribs = NativeMethods.SHGFAO.SFGAO_LINK | NativeMethods.SHGFAO.SFGAO_SHARE | NativeMethods.SHGFAO.SFGAO_HIDDEN | NativeMethods.SHGFAO.SFGAO_FILESYSTEM | NativeMethods.SHGFAO.SFGAO_FOLDER; m_desktopFolder.GetAttributesOf(1, pidls, ref attribs); uint dwflag = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_ICON; int dwAttr = 0; // Build Key into HashTable for this Item int key = index * 256; if ((attribs & NativeMethods.SHGFAO.SFGAO_LINK) != 0) { key = key | 1; dwflag = dwflag | NativeMethods.SHGFI_LINKOVERLAY; hasOverlay = true; } if ((attribs & NativeMethods.SHGFAO.SFGAO_SHARE) != 0) { key = key | 2; dwflag = dwflag | NativeMethods.SHGFI_ADDOVERLAYS; hasOverlay = true; } if (selectedIcon) { key = key | 4; dwflag = dwflag | NativeMethods.SHGFI_SELECTED; hasOverlay = true; } if (openIcon) { key = key | 8; dwflag = dwflag | NativeMethods.SHGFI_OPENICON; hasOverlay = true; // Not really an overlay, but handled the same } if (m_imageTable.ContainsKey(key)) { rVal = (int)m_imageTable[key]; } else if (!hasOverlay && (attribs & NativeMethods.SHGFAO.SFGAO_HIDDEN) == 0) // For non-overlay icons, we already have { rVal = (int)Math.Floor((double)key / 256); // the right index -- put in table m_imageTable[key] = rVal; } else // Don't have iconindex for an overlay, get it. { //item.m_isDisk = (item.m_path.Length == 3 && item.m_path.EndsWith(":\\")); if ((attribs & NativeMethods.SHGFAO.SFGAO_FILESYSTEM) != 0 && (attribs & NativeMethods.SHGFAO.SFGAO_FOLDER) == 0) { dwflag = dwflag | NativeMethods.SHGFI_USEFILEATTRIBUTES; dwAttr = dwAttr | NativeMethods.FILE_ATTRIBUTE_NORMAL; } NativeMethods.SHFILEINFO shfiSmall = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(pidl, dwAttr, ref shfiSmall, Marshal.SizeOf(shfiSmall), dwflag | NativeMethods.SHGFI_SMALLICON); NativeMethods.SHFILEINFO shfiLarge = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(pidl, dwAttr, ref shfiLarge, Marshal.SizeOf(shfiLarge), dwflag | NativeMethods.SHGFI_LARGEICON); lock (m_imageTable) { rVal = NativeMethods.ComCtl32.ImageList_ReplaceIcon(m_smallImageListHandle, -1, shfiSmall.hIcon); NativeMethods.ComCtl32.ImageList_ReplaceIcon(m_largeImageListHandle, -1, shfiLarge.hIcon); } NativeMethods.User32.DestroyIcon(shfiSmall.hIcon); NativeMethods.User32.DestroyIcon(shfiLarge.hIcon); m_imageTable[key] = rVal; } return(rVal); }
protected override void OnFileOK(object sender, EventArgs e) { if (m_selectedPidls != null && m_selectedPidls.Length > 0) { NativeMethods.SHGFAO attribs = NativeMethods.SHGFAO.SFGAO_STORAGE; m_currentFolder.GetAttributesOf(1, m_selectedPidls, ref attribs); if ((attribs & NativeMethods.SHGFAO.SFGAO_STORAGE) == 0) { ((NativeMethods.IShellBrowser) this).BrowseObject(m_selectedPidls[0], NativeMethods.SBSP_RELATIVE); return; } m_selectedPath = GetDisplayName(m_currentFolder, m_selectedPidls[0], NativeMethods.SHGNO.SHGDN_FORPARSING); DialogResult = DialogResult.OK; } else { IntPtr[] pidls = new IntPtr[] { m_pidlAbsCurrent }; NativeMethods.SHGFAO attribs = NativeMethods.SHGFAO.SFGAO_STORAGE; m_desktopFolder.GetAttributesOf(1, pidls, ref attribs); if ((attribs & NativeMethods.SHGFAO.SFGAO_STORAGE) == 0) { return; } string currentPath = GetDisplayName(m_desktopFolder, m_pidlAbsCurrent, NativeMethods.SHGNO.SHGDN_FORPARSING); string path = fileNameComboBox.Text; string trimmedPath = path.Trim(); if (path != trimmedPath) { m_ignoreFileNameChange = true; path = trimmedPath; fileNameComboBox.Text = trimmedPath; } if (string.IsNullOrEmpty(path)) { m_selectedPath = currentPath; DialogResult = DialogResult.OK; return; } bool isPathRooted = Path.IsPathRooted(path); if (!isPathRooted) { path = Path.Combine(currentPath, path); } if (path.Contains("..")) { path = Path.GetFullPath(path); } if (!CheckPathExists || Directory.Exists(path)) { m_selectedPath = path; DialogResult = DialogResult.OK; return; } string message = string.Format(@"The folder '{0}' isn't accessible. The folder may be located in an unavailable location, protected with a password, or the filename contains a / or \.", path); MessageBoxWithFocusRestore(message, MessageBoxButtons.OK, MessageBoxIcon.Information); fileNameComboBox.SelectAll(); } }