我有一份listView文件清单。我使用以下方法为他们每个人分配了一个图标:
private void SetDocumentIcon(ListViewItem item, FileInfo file)
{
Icon iconForFile = Icon.ExtractAssociatedIcon(file.FullName);
if (!documentsIconsImageList.Images.ContainsKey(file.Extension))
{
iconForFile = Icon.ExtractAssociatedIcon(file.FullName);
documentsIconsImageList.Images.Add(file.Extension, iconForFile);
}
item.ImageKey = file.Extension;
}
Run Code Online (Sandbox Code Playgroud)
我尝试将此方法用于文件夹,但失败了。据我所知,问题在于Icon.ExtractAssociatedIcon文件而不是文件夹。那么如何提取文件夹的图标呢?
谢谢。
SHGetStockIconInfo是正确的做法,并且不需要添加不必要的文件 IO。它并不比SHGetFileInfo.
这是一个示例类,其结构与 Evk 的类类似。需要注意的一些重要事项:
SHGetStockIconInfo(或什至SHGetFileInfo,就此而言)获得图标句柄时,必须通过调用 清除本机图标DestroyIcon(),否则您将造成资源泄漏。Icon.FromHandle(),对象会存储您提供给它的句柄,并将在以后的操作中使用它。这意味着如果您立即调用DestroyIcon()然后尝试对您刚刚创建的图标执行某些操作,则会导致异常。您可以通过使用Clone()获取Icon不依赖于原始本机句柄的来避免这种情况。public static class DefaultIcons
{
private static Icon folderIcon;
public static Icon FolderLarge => folderIcon ?? (folderIcon = GetStockIcon(SHSIID_FOLDER, SHGSI_LARGEICON));
private static Icon GetStockIcon(uint type, uint size)
{
var info = new SHSTOCKICONINFO();
info.cbSize = (uint)Marshal.SizeOf(info);
SHGetStockIconInfo(type, SHGSI_ICON | size, ref info);
var icon = (Icon)Icon.FromHandle(info.hIcon).Clone(); // Get a copy that doesn't use the original handle
DestroyIcon(info.hIcon); // Clean up native icon to prevent resource leak
return icon;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public struct SHSTOCKICONINFO
{
public uint cbSize;
public IntPtr hIcon;
public int iSysIconIndex;
public int iIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szPath;
}
[DllImport("shell32.dll")]
public static extern int SHGetStockIconInfo(uint siid, uint uFlags, ref SHSTOCKICONINFO psii);
[DllImport("user32.dll")]
public static extern bool DestroyIcon(IntPtr handle);
private const uint SHSIID_FOLDER = 0x3;
private const uint SHGSI_ICON = 0x100;
private const uint SHGSI_LARGEICON = 0x0;
private const uint SHGSI_SMALLICON = 0x1;
}
Run Code Online (Sandbox Code Playgroud)
我敢打赌还有其他方法,但我认为最容易实现的是在SHGetFileInfo您创建的临时文件夹上使用win api 函数。示例代码:
public static class DefaultIcons
{
private static readonly Lazy<Icon> _lazyFolderIcon = new Lazy<Icon>(FetchIcon, true);
public static Icon FolderLarge
{
get { return _lazyFolderIcon.Value; }
}
private static Icon FetchIcon()
{
var tmpDir = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())).FullName;
var icon = ExtractFromPath(tmpDir);
Directory.Delete(tmpDir);
return icon;
}
private static Icon ExtractFromPath(string path)
{
SHFILEINFO shinfo = new SHFILEINFO();
SHGetFileInfo(
path,
0, ref shinfo, (uint)Marshal.SizeOf(shinfo),
SHGFI_ICON | SHGFI_LARGEICON);
return System.Drawing.Icon.FromHandle(shinfo.hIcon);
}
//Struct used by SHGetFileInfo function
[StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("shell32.dll")]
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0;
private const uint SHGFI_SMALLICON = 0x000000001;
}
Run Code Online (Sandbox Code Playgroud)
用法只是
var icon = DefaultIcons.FolderLarge
Run Code Online (Sandbox Code Playgroud)
为小图标添加属性也很简单。