SystemIcon.cs 7.0 KB

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