/// <summary> /// Returns an icon for a given file - indicated by the name parameter. /// </summary> /// <param name="name">Pathname for file.</param> /// <param name="size">Large or small</param> /// <param name="linkOverlay">Whether to include the link icon</param> /// <returns>System.Drawing.Icon</returns> public static System.Drawing.Icon GetFileIcon(string name, IconSize size, bool linkOverlay) { Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY; /* Check the size specified for return. */ if (IconSize.Small == size) { flags += Shell32.SHGFI_SMALLICON ; } else { flags += Shell32.SHGFI_LARGEICON ; } Shell32.SHGetFileInfo( name, Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi), flags ); // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon( shfi.hIcon ); // Cleanup return icon; }
public static string GetDisplayName(string name, bool isDirectory) { uint flags = Shell32.SHGFI_TYPENAME | Shell32.SHGFI_USEFILEATTRIBUTES; Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); uint fileType = isDirectory ? Shell32.FILE_ATTRIBUTE_DIRECTORY : Shell32.FILE_ATTRIBUTE_NORMAL; Shell32.SHGetFileInfo(name, fileType, ref shfi, (uint)Marshal.SizeOf(shfi), flags); return shfi.szTypeName; }
/// <summary> /// Internal add a icon to class /// FOLDERS ONLY!!!!!! /// </summary> /// <param name="path">Icon Path on filesystem, or extension</param> /// <param name="iconSize">Icon Size</param> /// <param name="iconProp">Icon Properties to assign to list</param> /// <param name="folder">Folder type (open or closed)</param> private void Add(string path, IconReader.IconSize iconSize, IconProperties iconProp, IconReader.FolderType folder) { iconProp.IconsIndex[iconSize] = -1; iconProp.IconsInfo[iconSize] = new Shell32.SHFILEINFO(); Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); iconProp.Icons[iconSize] = IconReader.GetFolderIcon(iconSize, folder, ref shfi); iconProp.IconsInfo[iconSize] = shfi; if (IImageList[iconSize] != null) { iconProp.IconsIndex[iconSize] = IImageList[iconSize].Images.Count; IImageList[iconSize].Images.Add(path, iconProp.Icons[iconSize]); } }
/// <summary> /// Used to access system folder icons. /// </summary> /// <param name="size">Specify large or small icons.</param> /// <param name="folderType">Specify open or closed FolderType.</param> /// <returns>System.Drawing.Icon</returns> public static System.Drawing.Icon GetFolderIcon( IconSize size, FolderType folderType ) { // Need to add size check, although errors generated at present! uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; if (FolderType.Open == folderType) { flags += Shell32.SHGFI_OPENICON; } if (IconSize.Small == size) { flags += Shell32.SHGFI_SMALLICON; } else { flags += Shell32.SHGFI_LARGEICON; } // Get the folder icon Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); Shell32.SHGetFileInfo( ".?.", Shell32.FILE_ATTRIBUTE_DIRECTORY, ref shfi, (uint) System.Runtime.InteropServices.Marshal.SizeOf(shfi), flags ); System.Drawing.Icon.FromHandle(shfi.hIcon); // Load the icon from an HICON handle // Now clone the icon, so that it can be successfully stored in an ImageList System.Drawing.Icon icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon( shfi.hIcon ); // Cleanup return icon; }
/// <summary> /// Returns an icon for a given file - indicated by the name parameter. /// </summary> /// <param name="name">Pathname for file.</param> /// <param name="size">Large or small</param> /// <param name="linkOverlay">Whether to include the link icon</param> /// <returns>System.Drawing.Icon</returns> public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay) { uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES; if (true == linkOverlay) flags += Shell32.SHGFI_LINKOVERLAY; if (IconSize.Small == size) { flags += Shell32.SHGFI_SMALLICON; } else { flags += Shell32.SHGFI_LARGEICON; } Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO(); Shell32.SHGetFileInfo(name, Shell32.FILE_ATTRIBUTE_NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags); // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon(shfi.hIcon); // Cleanup return icon; }
/// <summary> /// Used to access system folder icons. /// </summary> /// <param name="size">Specify large or small icons.</param> /// <param name="folderType">Specify open or closed FolderType.</param> /// <param name="shfi">Return Folder Information</param> /// <returns>System.Drawing.Icon</returns> public static Icon GetFolderIcon(IconSize size, FolderType folderType, ref Shell32.SHFILEINFO shfi) { // Need to add size check, although errors generated at present! Shell32.SHGetFileInfoConstants flags = Shell32.SHGetFileInfoConstants.SHGFI_TYPENAME | Shell32.SHGetFileInfoConstants.SHGFI_DISPLAYNAME | Shell32.SHGetFileInfoConstants.SHGFI_ICON | Shell32.SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; if (FolderType.Open == folderType) { flags |= Shell32.SHGetFileInfoConstants.SHGFI_OPENICON; } if (IconSize.Small == size) { flags |= Shell32.SHGetFileInfoConstants.SHGFI_SMALLICON; } else { flags |= Shell32.SHGetFileInfoConstants.SHGFI_LARGEICON; } IntPtr hIml; // Get the folder icon shfi = new Shell32.SHFILEINFO(); if (IconHelper.Utils.IsSevenOrAbove()) // Windows 7 FIX { hIml = Shell32.SHGetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.System), Shell32.FILE_ATTRIBUTE.DIRECTORY, ref shfi, (uint)Marshal.SizeOf(shfi), flags); } else { hIml = Shell32.SHGetFileInfo(null, Shell32.FILE_ATTRIBUTE.DIRECTORY, ref shfi, (uint)Marshal.SizeOf(shfi), flags); } if (shfi.hIcon == IntPtr.Zero) { return(null); } if (!IconHelper.Utils.IsXpOrAbove()) { return(GetManagedIcon(shfi.hIcon)); } // Get the System IImageList object from the Shell: Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); Shell32.IImageList iImageList = null; int ret = Shell32.SHGetImageList( (int)size, ref iidImageList, ref iImageList ); // the image list handle is the IUnknown pointer, but // using Marshal.GetIUnknownForObject doesn't return // the right value. It really doesn't hurt to make // a second call to get the handle: Shell32.SHGetImageListHandle((int)size, ref iidImageList, ref hIml); IntPtr hIcon = IntPtr.Zero; if (iImageList == null) { hIcon = Comctl32.ImageList_GetIcon( hIml, shfi.iIcon, (int)Comctl32.ImageListDrawItemConstants.ILD_TRANSPARENT); } else { iImageList.GetIcon( shfi.iIcon, (int)Comctl32.ImageListDrawItemConstants.ILD_TRANSPARENT, ref hIcon); } return(hIcon == IntPtr.Zero ? GetManagedIcon(shfi.hIcon) : GetManagedIcon(hIcon)); }
/// <summary> /// Returns an icon for a given file - indicated by the name parameter. /// </summary> /// <param name="name">Extension or pathname for file.</param> /// <param name="size">Large or small</param> /// <param name="linkOverlay">Whether to include the link icon</param> /// <param name="shfi">Return File Information</param> /// <returns>System.Drawing.Icon</returns> public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay, ref Shell32.SHFILEINFO shfi) { name = Environment.ExpandEnvironmentVariables(name); shfi = new Shell32.SHFILEINFO(); Shell32.SHGetFileInfoConstants flags = Shell32.SHGetFileInfoConstants.SHGFI_TYPENAME | Shell32.SHGetFileInfoConstants.SHGFI_DISPLAYNAME | Shell32.SHGetFileInfoConstants.SHGFI_ICON | Shell32.SHGetFileInfoConstants.SHGFI_SHELLICONSIZE | Shell32.SHGetFileInfoConstants.SHGFI_SYSICONINDEX | Shell32.SHGetFileInfoConstants.SHGFI_USEFILEATTRIBUTES; if (linkOverlay) { flags |= Shell32.SHGetFileInfoConstants.SHGFI_LINKOVERLAY; } /* Check the size specified for return. */ if (IconSize.Small == size) { flags |= Shell32.SHGetFileInfoConstants.SHGFI_SMALLICON; } else { flags |= Shell32.SHGetFileInfoConstants.SHGFI_LARGEICON; } IntPtr hIml = Shell32.SHGetFileInfo(name, Shell32.FILE_ATTRIBUTE.NORMAL, ref shfi, (uint)Marshal.SizeOf(shfi), flags); if (shfi.hIcon == IntPtr.Zero) { return(null); } if (!IconHelper.Utils.IsXpOrAbove()) { return(GetManagedIcon(shfi.hIcon)); } // Get the System IImageList object from the Shell: Guid iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950"); Shell32.IImageList iImageList = null; int ret = Shell32.SHGetImageList( (int)size, ref iidImageList, ref iImageList ); // the image list handle is the IUnknown pointer, but // using Marshal.GetIUnknownForObject doesn't return // the right value. It really doesn't hurt to make // a second call to get the handle: Shell32.SHGetImageListHandle((int)size, ref iidImageList, ref hIml); IntPtr hIcon = IntPtr.Zero; if (iImageList == null) { hIcon = Comctl32.ImageList_GetIcon( hIml, shfi.iIcon, (int)Comctl32.ImageListDrawItemConstants.ILD_TRANSPARENT); } else { iImageList.GetIcon( shfi.iIcon, (int)Comctl32.ImageListDrawItemConstants.ILD_TRANSPARENT, ref hIcon); } return(hIcon == IntPtr.Zero ? GetManagedIcon(shfi.hIcon) : GetManagedIcon(hIcon)); }
/// <summary> /// Extract the icon from file, and return icon information /// </summary> /// <param name="path">File path, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param> /// <param name="size">The desired icon size</param> /// <param name="linkOverlay">Whether to include the link icon</param> /// <param name="shfi">The icon size</param> /// <returns>This method always returns an icon with the especified size and thier information.</returns> public static Icon ExtractIconFromFileEx(string path, IconSize size, bool linkOverlay, ref Shell32.SHFILEINFO shfi) { EmbeddedIconInfo embeddedIcon = GetEmbeddedIconInfo(Environment.ExpandEnvironmentVariables(path)); //Gets the handle of the icon. return(GetFileIcon(embeddedIcon.FileName, size, linkOverlay, ref shfi)); }