DefaultIcons.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace GeekDesk.Util
  9. {
  10. public static class DefaultIcons
  11. {
  12. private static Icon folderIcon;
  13. public static Icon FolderLarge => folderIcon ?? (folderIcon = GetStockIcon(SHGSI_ICON, SHGSI_LARGEICON));
  14. public static Icon GetStockIcon(uint type, uint size)
  15. {
  16. var info = new SHSTOCKICONINFO();
  17. info.cbSize = (uint)Marshal.SizeOf(info);
  18. SHGetStockIconInfo(type, SHGSI_ICON | size, ref info);
  19. var icon = (Icon)Icon.FromHandle(info.hIcon).Clone(); // Get a copy that doesn't use the original handle
  20. DestroyIcon(info.hIcon); // Clean up native icon to prevent resource leak
  21. return icon;
  22. }
  23. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  24. public struct SHSTOCKICONINFO
  25. {
  26. public uint cbSize;
  27. public IntPtr hIcon;
  28. public int iSysIconIndex;
  29. public int iIcon;
  30. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  31. public string szPath;
  32. }
  33. [DllImport("shell32.dll")]
  34. public static extern int SHGetStockIconInfo(uint siid, uint uFlags, ref SHSTOCKICONINFO psii);
  35. [DllImport("user32.dll")]
  36. public static extern bool DestroyIcon(IntPtr handle);
  37. public static uint SHSIID_FOLDER = 0x3;
  38. public static uint SHGSI_ICON = 0x100;
  39. public static uint SHGSI_LARGEICON = 0x0;
  40. public static uint SHGSI_SMALLICON = 0x1;
  41. }
  42. }