mirror of https://github.com/dahall/Vanara.git
Changed GetIcon to GetSystemIcon and added GetFileIcon.
parent
204f752545
commit
ca7ca983ca
|
@ -1,39 +1,58 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.IO;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
using static Vanara.PInvoke.ComCtl32;
|
using static Vanara.PInvoke.ComCtl32;
|
||||||
using static Vanara.PInvoke.Shell32;
|
using static Vanara.PInvoke.Shell32;
|
||||||
using static Vanara.PInvoke.User32_Gdi;
|
|
||||||
|
|
||||||
namespace Vanara.Windows.Shell
|
namespace Vanara.Windows.Shell
|
||||||
{
|
{
|
||||||
|
/// <summary>Used to determine the size of the icon returned by <see cref="ShellImageList.GetSystemIcon"/>.</summary>
|
||||||
|
public enum ShellImageSize
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The image size is normally 32x32 pixels. However, if the Use large icons option is selected from the Effects section of the Appearance tab in
|
||||||
|
/// Display Properties, the image is 48x48 pixels.
|
||||||
|
/// </summary>
|
||||||
|
Large = SHIL.SHIL_LARGE,
|
||||||
|
/// <summary>The image is the Shell standard small icon size of 16x16, but the size can be customized by the user.</summary>
|
||||||
|
Small = SHIL.SHIL_SMALL,
|
||||||
|
/// <summary>The image is the Shell standard extra-large icon size. This is typically 48x48, but the size can be customized by the user.</summary>
|
||||||
|
ExtraLarge = SHIL.SHIL_EXTRALARGE,
|
||||||
|
/// <summary>Windows Vista and later. The image is normally 256x256 pixels.</summary>
|
||||||
|
Jumbo = SHIL.SHIL_JUMBO
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Represents the System Image List holding images for all shell icons.</summary>
|
||||||
public static class ShellImageList
|
public static class ShellImageList
|
||||||
{
|
{
|
||||||
private static IntPtr largeImageListHandle;
|
private static int shfiSz = SHFILEINFO.Size;
|
||||||
private static IntPtr smallImageListHandle;
|
|
||||||
|
|
||||||
static ShellImageList()
|
/// <summary>Gets the system icon for the given file name or extension.</summary>
|
||||||
|
/// <param name="fileNameOrExtension">The file name or extension.</param>
|
||||||
|
/// <param name="iconSize">Size of the icon.</param>
|
||||||
|
/// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
|
||||||
|
public static Icon GetSystemIcon(string fileNameOrExtension, ShellImageSize iconSize = ShellImageSize.Large)
|
||||||
{
|
{
|
||||||
const SHGFI baseFlags = SHGFI.SHGFI_USEFILEATTRIBUTES | SHGFI.SHGFI_SYSICONINDEX;
|
var shfi = new SHFILEINFO();
|
||||||
|
var hImageList = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, SHGFI.SHGFI_SYSICONINDEX | (iconSize == ShellImageSize.Small ? SHGFI.SHGFI_SMALLICON : 0));
|
||||||
var shfiSz = Marshal.SizeOf(typeof(SHFILEINFO));
|
if (hImageList == IntPtr.Zero) return null;
|
||||||
|
if (iconSize <= ShellImageSize.Small)
|
||||||
var shfiSmall = new SHFILEINFO();
|
return IconLocation.GetClonedIcon(ImageList_GetIcon(hImageList, shfi.iIcon, IMAGELISTDRAWFLAGS.ILD_TRANSPARENT));
|
||||||
smallImageListHandle = SHGetFileInfo(".txt", FileAttributes.Normal, ref shfiSmall, shfiSz, baseFlags | SHGFI.SHGFI_SMALLICON);
|
SHGetImageList((SHIL)iconSize, typeof(IImageList).GUID, out var il).ThrowIfFailed();
|
||||||
|
return IconLocation.GetClonedIcon(il.GetIcon(shfi.iIcon, IMAGELISTDRAWFLAGS.ILD_TRANSPARENT));
|
||||||
var shfiLarge = new SHFILEINFO();
|
|
||||||
largeImageListHandle = SHGetFileInfo(".txt", FileAttributes.Normal, ref shfiLarge, shfiSz, baseFlags | SHGFI.SHGFI_LARGEICON);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Icon GetIcon(int index, bool small = true)
|
/// <summary>Gets the Shell icon for the given file name or extension.</summary>
|
||||||
|
/// <param name="fileNameOrExtension">The file name or extension .</param>
|
||||||
|
/// <param name="iconType">Flags to specify the type of the icon to retrieve. This uses the <see cref="SHGetFileInfo(string, System.IO.FileAttributes, ref SHFILEINFO, int, SHGFI)"/> method and can only retrieve small or large icons.</param>
|
||||||
|
/// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
|
||||||
|
public static Icon GetFileIcon(string fileNameOrExtension, ShellIconType iconType = ShellIconType.Large)
|
||||||
{
|
{
|
||||||
var hIcon = ImageList_GetIcon(small ? smallImageListHandle : largeImageListHandle, index, IMAGELISTDRAWFLAGS.ILD_NORMAL);
|
const SHGFI baseFlags = SHGFI.SHGFI_USEFILEATTRIBUTES | SHGFI.SHGFI_ICON;
|
||||||
if (hIcon == IntPtr.Zero) return null;
|
var shfi = new SHFILEINFO();
|
||||||
var icon = Icon.FromHandle(hIcon);
|
var ret = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, baseFlags | (SHGFI)iconType);
|
||||||
var ret = (Icon)icon.Clone();
|
if (ret == IntPtr.Zero)
|
||||||
DestroyIcon(hIcon);
|
ret = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, SHGFI.SHGFI_ICON | (SHGFI)iconType);
|
||||||
return ret;
|
return ret == IntPtr.Zero ? null : IconLocation.GetClonedIcon(shfi.hIcon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue