SystemIcon.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using Microsoft.Win32;
  8. using System.Runtime.InteropServices;
  9. using System.Drawing.Imaging;
  10. using GeekDesk.Util;
  11. namespace GeekDesk.Util
  12. {
  13. class SystemIcon
  14. {
  15. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  16. public struct SHFILEINFO
  17. {
  18. public IntPtr hIcon;
  19. public int iIcon;
  20. public uint dwAttributes;
  21. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  22. public string szDisplayName;
  23. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
  24. public string szTypeName;
  25. }
  26. [DllImport("Shell32.dll", EntryPoint = "SHGetFileInfo", SetLastError = true, CharSet = CharSet.Auto)]
  27. public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
  28. [DllImport("User32.dll", EntryPoint = "DestroyIcon")]
  29. public static extern int DestroyIcon(IntPtr hIcon);
  30. #region API 参数的常量定义
  31. public enum FileInfoFlags : uint
  32. {
  33. SHGFI_ICON = 0x000000100, // get icon
  34. SHGFI_DISPLAYNAME = 0x000000200, // get display name
  35. SHGFI_TYPENAME = 0x000000400, // get type name
  36. SHGFI_ATTRIBUTES = 0x000000800, // get attributes
  37. SHGFI_ICONLOCATION = 0x000001000, // get icon location
  38. SHGFI_EXETYPE = 0x000002000, // return exe type
  39. SHGFI_SYSICONINDEX = 0x000004000, // get system icon index
  40. SHGFI_LINKOVERLAY = 0x000008000, // put a link overlay on icon
  41. SHGFI_SELECTED = 0x000010000, // show icon in selected state
  42. SHGFI_ATTR_SPECIFIED = 0x000020000, // get only specified attributes
  43. SHGFI_LARGEICON = 0x000000000, // get large icon
  44. SHGFI_SMALLICON = 0x000000001, // get small icon
  45. SHGFI_OPENICON = 0x000000002, // get open icon
  46. SHGFI_SHELLICONSIZE = 0x000000004, // get shell size icon
  47. SHGFI_PIDL = 0x000000008, // pszPath is a pidl
  48. SHGFI_USEFILEATTRIBUTES = 0x000000010, // use passed dwFileAttribute
  49. SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays
  50. SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay
  51. }
  52. public enum FileAttributeFlags : uint
  53. {
  54. FILE_ATTRIBUTE_READONLY = 0x00000001,
  55. FILE_ATTRIBUTE_HIDDEN = 0x00000002,
  56. FILE_ATTRIBUTE_SYSTEM = 0x00000004,
  57. FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
  58. FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
  59. FILE_ATTRIBUTE_DEVICE = 0x00000040,
  60. FILE_ATTRIBUTE_NORMAL = 0x00000080,
  61. FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
  62. FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
  63. FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
  64. FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
  65. FILE_ATTRIBUTE_OFFLINE = 0x00001000,
  66. FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
  67. FILE_ATTRIBUTE_ENCRYPTED = 0x00004000
  68. }
  69. #endregion
  70. /// <summary>
  71. /// 获取文件类型的关联图标
  72. /// </summary>
  73. /// <param name="fileName">文件类型的扩展名或文件的绝对路径</param>
  74. /// <param name="isLargeIcon">是否返回大图标</param>
  75. /// <returns>获取到的图标</returns>
  76. public static Icon GetIcon(string fileName, bool isLargeIcon)
  77. {
  78. //SHFILEINFO shfi = new SHFILEINFO();
  79. //IntPtr hI;
  80. //if (isLargeIcon)
  81. // hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)FileInfoFlags.SHGFI_LARGEICON);
  82. //else
  83. // hI = SHGetFileInfo(fileName, 0, ref shfi, (uint)Marshal.SizeOf(shfi), (uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_USEFILEATTRIBUTES | (uint)FileInfoFlags.SHGFI_SMALLICON);
  84. //Icon icon = Icon.FromHandle(shfi.hIcon).Clone() as Icon;
  85. //DestroyIcon(shfi.hIcon); //释放资源
  86. //选中文件中的图标总数
  87. //var iconTotalCount = PrivateExtractIcons(fileName, 0, 0, 0, null, null, 1, 0);
  88. //用于接收获取到的图标指针
  89. //IntPtr[] hIcons = new IntPtr[1];
  90. ////对应的图标id
  91. //int[] ids = new int[1];
  92. ////成功获取到的图标个数
  93. //int successCount = PrivateExtractIcons(fileName, 0, 0, 0, hIcons, ids, 1, 0);
  94. //Icon ico = Icon.FromHandle(hIcons[0]);
  95. //var myIcon = ico.ToBitmap();
  96. //myIcon.Save("D:\\" + ids[0].ToString("000") + ".png", ImageFormat.Png);
  97. IntPtr hIcon = FileIcon.GetJumboIcon(FileIcon.GetIconIndex(fileName));
  98. //IntPtr hIcon = GetJumboIcon(GetIconIndex("*." + ext));
  99. // from native to managed
  100. Icon ico = Icon.FromHandle(hIcon);
  101. string path = "D:\\test\\" + System.Guid.NewGuid().ToString() + ".png";
  102. //using ( ico = (Icon)Icon.FromHandle(hIcon).Clone())
  103. //{
  104. // // save to file (or show in a picture box)
  105. // ico.ToBitmap().Save(path, ImageFormat.Png);
  106. //}
  107. //FileIcon.Shell32.DestroyIcon(hIcon); // don't forget to cleanup
  108. return ico;
  109. }
  110. /// <summary>
  111. /// 获取文件夹图标
  112. /// </summary>
  113. /// <returns>图标</returns>
  114. public static Icon GetDirectoryIcon(bool isLargeIcon)
  115. {
  116. SHFILEINFO _SHFILEINFO = new SHFILEINFO();
  117. IntPtr _IconIntPtr;
  118. if (isLargeIcon)
  119. {
  120. _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), ((uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_LARGEICON));
  121. }
  122. else
  123. {
  124. _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), ((uint)FileInfoFlags.SHGFI_ICON | (uint)FileInfoFlags.SHGFI_SMALLICON));
  125. }
  126. if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
  127. Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
  128. return _Icon;
  129. }
  130. [DllImport("User32.dll")]
  131. public static extern int PrivateExtractIcons(
  132. string lpszFile, //file name
  133. int nIconIndex, //The zero-based index of the first icon to extract.
  134. int cxIcon, //The horizontal icon size wanted.
  135. int cyIcon, //The vertical icon size wanted.
  136. IntPtr[] phicon, //(out) A pointer to the returned array of icon handles.
  137. int[] piconid, //(out) A pointer to a returned resource identifier.
  138. int nIcons, //The number of icons to extract from the file. Only valid when *.exe and *.dll
  139. int flags //Specifies flags that control this function.
  140. );
  141. //[DllImport("User32.dll")]
  142. //public static extern bool DestroyIcon(
  143. // IntPtr hIcon //A handle to the icon to be destroyed. The icon must not be in use.
  144. // );
  145. }
  146. }