static ShellImageList() { IntPtr desktopFolderPtr; NativeMethods.Shell32.SHGetDesktopFolder(out desktopFolderPtr); m_desktopFolder = (NativeMethods.IShellFolder)Marshal.GetObjectForIUnknown(desktopFolderPtr); m_imageTable = new Hashtable(); uint flag = NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_SMALLICON; NativeMethods.SHFILEINFO shfiSmall = new NativeMethods.SHFILEINFO(); m_smallImageListHandle = NativeMethods.Shell32.SHGetFileInfo(".txt", NativeMethods.FILE_ATTRIBUTE_NORMAL, ref shfiSmall, Marshal.SizeOf(shfiSmall), flag); flag = NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_LARGEICON; NativeMethods.SHFILEINFO shfiLarge = new NativeMethods.SHFILEINFO(); m_largeImageListHandle = NativeMethods.Shell32.SHGetFileInfo(".txt", NativeMethods.FILE_ATTRIBUTE_NORMAL, ref shfiLarge, Marshal.SizeOf(shfiLarge), flag); }
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; }
public static Image GetImage(IntPtr pidl) { uint dwflag = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_ICON; int dwAttr = 0; NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(pidl, dwAttr, ref shfi, Marshal.SizeOf(shfi), dwflag); IntPtr iconPtr = NativeMethods.ComCtl32.ImageList_GetIcon(m_largeImageListHandle, shfi.iIcon, NativeMethods.ILD_NORMAL); return Icon.FromHandle(iconPtr).ToBitmap(); }
public static Image GetSmallImage(SpecialFolder specialFolder) { uint dwflag = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_ICON; int dwAttr = 0; IntPtr pidl; NativeMethods.Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, (int)specialFolder, out pidl); NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(pidl, dwAttr, ref shfi, Marshal.SizeOf(shfi), dwflag); IntPtr iconPtr = NativeMethods.ComCtl32.ImageList_GetIcon(m_smallImageListHandle, shfi.iIcon, NativeMethods.ILD_NORMAL); return Icon.FromHandle(iconPtr).ToBitmap(); }
private static NativeMethods.SHGFAO GetAttributesOf(IntPtr abspidl) { NativeMethods.SHFILEINFO sfi = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(abspidl, NativeMethods.FILE_ATTRIBUTE_NORMAL, ref sfi, NativeMethods.cbFileInfo, NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_ATTRIBUTES); return (NativeMethods.SHGFAO)sfi.dwAttributes; }
// Allows the common dialog box to filter objects that the view displays. int NativeMethods.ICommDlgBrowser.IncludeObject(IntPtr ppshv, IntPtr pidl) { IntPtr strr = Marshal.AllocCoTaskMem(NativeMethods.MAX_PATH * 2 + 4); Marshal.WriteInt32(strr, 0, 0); StringBuilder buf = new StringBuilder(NativeMethods.MAX_PATH); IntPtr[] pidls = new IntPtr[] { pidl }; NativeMethods.SHGFAO attribs = NativeMethods.SHGFAO.SFGAO_FILESYSANCESTOR | NativeMethods.SHGFAO.SFGAO_STREAM | NativeMethods.SHGFAO.SFGAO_LINK; m_currentFolder.GetAttributesOf(1, pidls, ref attribs); bool isFile = ((attribs & NativeMethods.SHGFAO.SFGAO_STREAM) == NativeMethods.SHGFAO.SFGAO_STREAM); bool isLink = ((attribs & NativeMethods.SHGFAO.SFGAO_LINK) == NativeMethods.SHGFAO.SFGAO_LINK); if (m_excludeFiles && !isLink && (isFile || (attribs & NativeMethods.SHGFAO.SFGAO_FILESYSANCESTOR) == 0)) return NativeMethods.S_FALSE; if (!isFile && (attribs & NativeMethods.SHGFAO.SFGAO_FILESYSANCESTOR) == 0) return NativeMethods.S_FALSE; if (m_currentFolder.GetDisplayNameOf( pidl, NativeMethods.SHGNO.SHGDN_FORPARSING, strr) == NativeMethods.S_OK) { NativeMethods.Shlwapi.StrRetToBuf(strr, pidl, buf, NativeMethods.MAX_PATH); string path = buf.ToString(); string name = path.Substring(path.LastIndexOf('\\') + 1); // If the object is a newly created folder browser to that folder if (CreateNewFolder(path, pidl)) return NativeMethods.S_OK; if (isLink && File.Exists(path)) { NativeMethods.IShellLink psl; psl = (NativeMethods.IShellLink)new NativeMethods.ShellLink(); NativeMethods.IPersistFile ppf; ppf = (NativeMethods.IPersistFile)psl; ppf.Load(path, 0); IntPtr linkPidl; psl.GetIDList(out linkPidl); // IShellFolder.GetAttributesOf doesn't work NativeMethods.SHFILEINFO sfi = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(linkPidl, NativeMethods.FILE_ATTRIBUTE_NORMAL, ref sfi, NativeMethods.cbFileInfo, NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_ATTRIBUTES); string linkPath = GetDisplayName(m_desktopFolder, linkPidl, NativeMethods.SHGNO.SHGDN_FORPARSING); NativeMethods.Shell32.ILFree(linkPidl); Marshal.ReleaseComObject(ppf); Marshal.ReleaseComObject(psl); if ((sfi.dwAttributes & (int)NativeMethods.SHGFAO.SFGAO_FOLDER) == (int)NativeMethods.SHGFAO.SFGAO_FOLDER) return NativeMethods.S_OK; if ((sfi.dwAttributes & (int)NativeMethods.SHGFAO.SFGAO_STREAM) == (int)NativeMethods.SHGFAO.SFGAO_STREAM) { if (m_excludeFiles) return NativeMethods.S_FALSE; string linkName = linkPath.Substring(linkPath.LastIndexOf('\\') + 1); FileType linkFileType = (FileType)fileTypeComboBox.SelectedItem; if (m_currentFilePattern != string.Empty) { // If the object is a file and the file doesn't match the pattern, don't include the object if (isFile && !Regex.IsMatch(linkName, m_currentFilePattern, RegexOptions.Compiled)) return NativeMethods.S_FALSE; } else if (linkFileType != null) { if (linkFileType.IncludeAllFiles) return NativeMethods.S_OK; // If the object is a file and the file doesn't math the pattern, don't include the object if (isFile && !Regex.IsMatch(name, linkFileType.FilterPattern, RegexOptions.Compiled)) return NativeMethods.S_FALSE; } return NativeMethods.S_OK; } } if (m_excludeFiles) return NativeMethods.S_OK; FileType fileType = (FileType)fileTypeComboBox.SelectedItem; if (m_currentFilePattern != string.Empty) { // If the object is a file and the file doesn't match the pattern, don't include the object if (isFile && !Regex.IsMatch(name, m_currentFilePattern, RegexOptions.Compiled)) return NativeMethods.S_FALSE; } else if (fileType != null) { if (fileType.IncludeAllFiles) return NativeMethods.S_OK; // If the object is a file and the file doesn't math the pattern, don't include the object if (isFile && !Regex.IsMatch(name, fileType.FilterPattern, RegexOptions.Compiled)) return NativeMethods.S_FALSE; } } return NativeMethods.S_OK; }
private void Init() { // My Computer NativeMethods.SHFILEINFO info = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, (int)SpecialFolder.MyComputer, out m_myCompPidl); NativeMethods.Shell32.SHGetFileInfo(m_myCompPidl, 0, ref info, NativeMethods.cbFileInfo, NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_DISPLAYNAME); // Desktop NativeMethods.Shell32.SHGetSpecialFolderLocation(IntPtr.Zero, (int)SpecialFolder.Desktop, out m_desktopPidl); IntPtr desktopFolderPtr; NativeMethods.Shell32.SHGetDesktopFolder(out desktopFolderPtr); m_desktopFolder = (NativeMethods.IShellFolder)Marshal.GetObjectForIUnknown(desktopFolderPtr); m_desktopPath = GetDisplayName(m_desktopFolder, m_desktopPidl, NativeMethods.SHGNO.SHGDN_FORPARSING); m_currentFolder = m_desktopFolder; // My Documents m_desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, NativeMethods.CLSID_MyDocuments, IntPtr.Zero, out m_myDocsPidl, 0); info = new NativeMethods.SHFILEINFO(); NativeMethods.Shell32.SHGetFileInfo(m_myDocsPidl, 0, ref info, NativeMethods.cbFileInfo, NativeMethods.SHGFI_PIDL | NativeMethods.SHGFI_DISPLAYNAME); m_myDocsPath = GetDisplayName(m_desktopFolder, m_myDocsPidl, NativeMethods.SHGNO.SHGDN_FORPARSING); }