封装:Windows系统文件图标

2021-01-23 13:15

阅读:755

标签:dllimport   ptr   summary   end   iter   region   span   ESS   class   

原文:封装:Windows系统文件图标

技术图片

用途:用于获取文件系统默认图标

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace HebianGu.ComLibModule.API
  9. {
  10. /// 获取文件关联图标
  11. public partial class IconHelper
  12. {
  13. /// 返回系统设置的图标
  14. /// 文件路径 如果为"" 返回文件夹的
  15. /// 0
  16. /// 结构体
  17. /// 结构体大小
  18. /// 枚举类型
  19. /// -1失败
  20. [DllImport("shell32.dll")]
  21. public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
  22. /// 获取文件图标
  23. public Icon GetFileIcon(string p_Path)
  24. {
  25. SHFILEINFO _SHFILEINFO = new SHFILEINFO();
  26. IntPtr _IconIntPtr = SHGetFileInfo(p_Path, 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES));
  27. if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
  28. Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
  29. return _Icon;
  30. }
  31. /// 获取文件夹图标
  32. public Icon GetDirectoryIcon()
  33. {
  34. SHFILEINFO _SHFILEINFO = new SHFILEINFO();
  35. IntPtr _IconIntPtr = SHGetFileInfo(@"", 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON));
  36. if (_IconIntPtr.Equals(IntPtr.Zero)) return null;
  37. Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);
  38. return _Icon;
  39. }
  40. /// 获取系统图标
  41. public Icon GetSystemInfoIcon(string p_Path)
  42. {
  43. if (Path.HasExtension(p_Path))
  44. {
  45. try
  46. {
  47. return Icon.ExtractAssociatedIcon(p_Path);
  48. }
  49. catch
  50. {
  51. return null;
  52. }
  53. }
  54. else
  55. {
  56. return this.GetDirectoryIcon();
  57. }
  58. }
  59. }
  60. partial class IconHelper
  61. {
  62. #region - Start 单例模式 -
  63. /// 单例模式
  64. private static IconHelper t = null;
  65. /// 多线程锁
  66. private static object localLock = new object();
  67. /// 创建指定对象的单例实例
  68. public static IconHelper Instance
  69. {
  70. get
  71. {
  72. if (t == null)
  73. {
  74. lock (localLock)
  75. {
  76. if (t == null)
  77. return t = new IconHelper();
  78. }
  79. }
  80. return t;
  81. }
  82. }
  83. /// 禁止外部实例
  84. private IconHelper()
  85. {
  86. }
  87. #endregion - 单例模式 End -
  88. }
  89. [StructLayout(LayoutKind.Sequential)]
  90. public struct SHFILEINFO
  91. {
  92. public IntPtr hIcon;
  93. public IntPtr iIcon;
  94. public uint dwAttributes;
  95. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  96. public string szDisplayName;
  97. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
  98. public string szTypeName;
  99. }
  100. public enum SHGFI
  101. {
  102. SHGFI_ICON = 0x100,
  103. SHGFI_LARGEICON = 0x0,
  104. SHGFI_USEFILEATTRIBUTES = 0x10
  105. }
  106. }

应用示例:

  1. /// 图片路径
  2. public Icon ImagePath
  3. {
  4. get { return IconHelper.Instance.GetSystemInfoIcon(FilePath); }
  5. }

  • 点赞 1
  • 收藏
  • 分享
    • 文章举报
技术图片技术图片
河边骨丶
发布了73 篇原创文章 · 获赞 30 · 访问量 6万+
私信 关注

封装:Windows系统文件图标

标签:dllimport   ptr   summary   end   iter   region   span   ESS   class   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12073139.html


评论


亲,登录后才可以留言!