我用下面的代码,不知道这是否透明背景的支持,但你可以试试看:
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0;
private const uint SHGFI_SMALLICON = 0x1;
private const uint SHGFI_DISPLAYNAME = 0x00000200;
private const uint SHGFI_TYPENAME = 0x400;
public static Icon GetSmallFileIcon(this FileInfo file)
{
if (file.Exists)
{
SHFILEINFO shFileInfo = new SHFILEINFO();
SHGetFileInfo(file.FullName, 0, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), SHGFI_ICON | SHGFI_SMALLICON);
return Icon.FromHandle(shFileInfo.hIcon);
}
else return SystemIcons.WinLogo;
}
public static Icon GetSmallFileIcon(string fileName)
{
return GetSmallFileIcon(new FileInfo(fileName));
}
public static Icon GetLargeFileIcon(this FileInfo file)
{
if (file.Exists)
{
SHFILEINFO shFileInfo = new SHFILEINFO();
SHGetFileInfo(file.FullName, 0, ref shFileInfo, (uint)Marshal.SizeOf(shFileInfo), SHGFI_ICON | SHGFI_LARGEICON);
return Icon.FromHandle(shFileInfo.hIcon);
}
else return SystemIcons.WinLogo;
}
public static Icon GetLargeFileIcon(string fileName)
{
return GetLargeFileIcon(new FileInfo(fileName));
}
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero; iIcon = IntPtr.Zero; dwAttributes = 0; szDisplayName = ""; szTypeName = "";
}
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
感谢您的摘录。其实我知道SHGetFileInfo函数。我只是想避免检查缩略图是否可用,如果不是提取图标。 – Giorgi 2010-12-22 16:02:36