C#获取文件(磁盘驱动器)的关联图标(使用API SHGetFileInfo)
生活随笔
收集整理的這篇文章主要介紹了
C#获取文件(磁盘驱动器)的关联图标(使用API SHGetFileInfo)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
這是一個(gè)C#調(diào)用系統(tǒng)API SHGetFileInfo 的一個(gè)演示例子,也是給一位網(wǎng)友的答復(fù),先看效果圖:
SHGetFileInfo 這個(gè)API, 可以獲取指定對(duì)象的非常詳細(xì)的相關(guān)信息,具體的內(nèi)容,大家可以MSDN上關(guān)于此API的說(shuō)明。
這個(gè)獲取關(guān)聯(lián)圖標(biāo),可以獲取磁盤(pán)分區(qū)的圖標(biāo),可以獲取某個(gè)特定類(lèi)型的文件的圖標(biāo),也可以獲取某個(gè)指定文件的圖標(biāo),下面給出實(shí)現(xiàn)的全部代碼:
/// <summary> /// 保存文件信息的結(jié)構(gòu)體 /// </summary> /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] 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; }class NativeMethods {[DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);[DllImport("User32.dll", EntryPoint = "DestroyIcon")]public static extern int DestroyIcon(IntPtr hIcon);#region API 參數(shù)的常量定義public const uint SHGFI_ICON = 0x100;public const uint SHGFI_LARGEICON = 0x0; //大圖標(biāo) 32×32public const uint SHGFI_SMALLICON = 0x1; //小圖標(biāo) 16×16public const uint SHGFI_USEFILEATTRIBUTES = 0x10;#endregion} /// <summary> /// 獲取文件類(lèi)型的關(guān)聯(lián)圖標(biāo) /// </summary> /// <param name="fileName">文件類(lèi)型的擴(kuò)展名或文件的絕對(duì)路徑</param> /// <param name="isLargeIcon">是否返回大圖標(biāo)</param> /// <returns>獲取到的圖標(biāo)</returns> static Icon GetIcon(string fileName, bool isLargeIcon) {SHFILEINFO shfi = new SHFILEINFO();IntPtr hI;if (isLargeIcon)hI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_LARGEICON);elsehI = NativeMethods.SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_USEFILEATTRIBUTES | NativeMethods.SHGFI_SMALLICON);Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;NativeMethods.DestroyIcon(shfi.hIcon); //釋放資源return icon; }private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {Help.ShowHelp(this, "http://www.zu14.cn"); }private void btnGetIcon_Click(object sender, EventArgs e) {using (Graphics g = this.pbSmallIcon.CreateGraphics()){g.Clear(this.pbSmallIcon.BackColor); //清除picturebox的背景色,為了畫(huà)透明圖標(biāo)g.DrawIcon(GetIcon(this.tbFileExt.Text, false), 1, 1); //繪制小圖標(biāo)}using (Graphics g = this.pbLargeIcon.CreateGraphics()){g.Clear(this.pbLargeIcon.BackColor); //清除picturebox的背景色,為了畫(huà)透明圖標(biāo)g.DrawIcon(GetIcon(this.tbFileExt.Text, true), 1, 1); //繪制小圖標(biāo)} }轉(zhuǎn)載于:https://www.cnblogs.com/sjcatsoft/archive/2009/04/04/1429479.html
總結(jié)
以上是生活随笔為你收集整理的C#获取文件(磁盘驱动器)的关联图标(使用API SHGetFileInfo)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 不使用第三个变量交换两个变量
- 下一篇: c# char unsigned_dll