C# 獲取系統(tǒng)圖標類
C# 獲取系統(tǒng)圖標類
來源 http://blog.5d.cn/vip/allinhands/200505/83959.html
代碼
using System;
using System.IO;
using System.Drawing;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace ThunderStarter
{
///
/// 提供從操作系統(tǒng)讀取圖標的方法
///
public class GetSystemIcon
{
///
/// 依據(jù)文件名讀取圖標,若指定文件不存在,則返回空值。
///
///
///
public Icon GetIconByFileName(string fileName)
{
if (fileName == null || fileName.Equals(string.Empty)) return null;
if (!File.Exists(fileName)) return null;
SHFILEINFO shinfo = new SHFILEINFO();
//Use this to get the small Icon
Win32.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
//The icon is returned in the hIcon member of the shinfo struct
System.Drawing.Icon myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
return myIcon;
}
///
/// 給出文件擴展名(.*),返回相應(yīng)圖標
/// 若不以"."開頭則返回文件夾的圖標。
///
///
///
///
public Icon GetIconByFileType(string fileType, bool isLarge)
{
if (fileType == null || fileType.Equals(string.Empty)) return null;
RegistryKey regVersion = null;
string regFileType = null;
string regIconString = null;
string systemDirectory = Environment.SystemDirectory + "\\";
if (fileType[0] == '.')
{
//讀系統(tǒng)注冊表中文件類型信息
regVersion = Registry.ClassesRoot.OpenSubKey(fileType, true);
if (regVersion != null)
{
regFileType = regVersion.GetValue("") as string;
regVersion.Close();
regVersion = Registry.ClassesRoot.OpenSubKey(regFileType + @"\DefaultIcon", true);
if (regVersion != null)
{
regIconString = regVersion.GetValue("") as string;
regVersion.Close();
}
}
if (regIconString == null)
{
//沒有讀取到文件類型注冊信息,指定為未知文件類型的圖標
regIconString = systemDirectory + "shell32.dll,0";
}
}
else
{
//直接指定為文件夾圖標
regIconString = systemDirectory + "shell32.dll,3";
}
string[] fileIcon = regIconString.Split(new char[] { ',' });
if (fileIcon.Length != 2)
{
//系統(tǒng)注冊表中注冊的標圖不能直接提取,則返回可執(zhí)行文件的通用圖標
fileIcon = new string[] { systemDirectory + "shell32.dll", "2" };
}
Icon resultIcon = null;
try
{
//調(diào)用API方法讀取圖標
int[] phiconLarge = new int[1];
int[] phiconSmall = new int[1];
uint count = Win32.ExtractIconEx(fileIcon[0], Int32.Parse(fileIcon[1]), phiconLarge, phiconSmall, 1);
IntPtr IconHnd = new IntPtr(isLarge ? phiconLarge[0] : phiconSmall[0]);
resultIcon = Icon.FromHandle(IconHnd);
}
catch { }
return resultIcon;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
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;
};
///
/// 定義調(diào)用的API方法
///
class Win32
{
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[DllImport("shell32.dll")]
public static extern uint ExtractIconEx(string lpszFile, int nIconIndex, int[] phiconLarge, int[] phiconSmall, uint nIcons);
}
}

浙公網(wǎng)安備 33010602011771號