/// <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 SIcon GetFileIcon(string name, IconSize size, bool linkOverlay) { var shfi = new Shell32.Shfileinfo(); var flags = Shell32.ShgfiIcon | Shell32.ShgfiUsefileattributes; if (linkOverlay) { flags += Shell32.ShgfiLinkoverlay; } /* Check the size specified for return. */ if (IconSize.Small == size) { flags += Shell32.ShgfiSmallicon; } else { flags += Shell32.ShgfiLargeicon; } Shell32.SHGetFileInfo(name, Shell32.FileAttributeNormal, ref shfi, (uint)Marshal.SizeOf(shfi), flags); // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly var icon = (SIcon)SIcon.FromHandle(shfi.hIcon).Clone(); User32.DestroyIcon(shfi.hIcon); // Cleanup return(icon); }
public static SDIcon GetIconFromList(IntPtr iconList, int index, int flags = ILD_TRANSPARENT) { IntPtr hIcon = ImageList_GetIcon(iconList, index, flags); if (hIcon != IntPtr.Zero) { var managedIcon = SDIcon.FromHandle(hIcon).Clone() as SDIcon; DestroyIcon(hIcon); return(managedIcon); } return(null); }
/// <summary> /// The meat and potatoes of the get icon functions above. Gets an icon based on the /// given path and flags. Will attempt to fetch icons from Image Lists if they use /// those (eg: .lnk/.url files). /// </summary> /// <param name="path">The path to get the icon for.</param> /// <param name="flags">A bitfield representing flags to pass to /// <see cref="Trampolines.ShellGetFileInfo(string, FileAttributes, uint)"/></param> /// <param name="attribs">FileAttributes of the given path.</param> /// <returns>The requested icon.</returns> private static SDIcon GetIcon(string path, uint flags, FileAttributes attribs = 0) { (SHFILEINFO info, IntPtr list) = Trampolines.ShellGetFileInfo(path, attribs, flags); SDIcon icon = null; if (info.hIcon != IntPtr.Zero) { icon = SDIcon.FromHandle(info.hIcon).Clone() as SDIcon; DestroyIcon(info.hIcon); return(icon); } else if (list != IntPtr.Zero && info.iIcon != IntPtr.Zero) { icon = Trampolines.GetIconFromList(list, info.iIcon.ToInt32()); } return(icon); }