IconManager.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #region License information
  2. // ----------------------------------------------------------------------------
  3. //
  4. // Eq2VpkTool - A tool to extract Everquest II VPK files
  5. // Blaz (blaz@blazlabs.com)
  6. //
  7. // This program is free software; you can redistribute it and/or
  8. // modify it under the terms of the GNU General Public License
  9. // as published by the Free Software Foundation; either version 2
  10. // of the License, or (at your option) any later version.
  11. //
  12. // This program is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. // GNU General Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public License
  18. // along with this program; if not, write to the Free Software
  19. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. //
  21. // ( The full text of the license can be found in the License.txt file )
  22. //
  23. // ----------------------------------------------------------------------------
  24. #endregion
  25. #region Using directives
  26. using System;
  27. using System.Drawing;
  28. using System.Runtime.InteropServices;
  29. using System.Windows.Forms;
  30. using Eq2FileSystemInfo = Everquest2.IO.FileSystemInfo;
  31. using Eq2FileInfo = Everquest2.IO.FileInfo;
  32. using Eq2DirectoryInfo = Everquest2.IO.DirectoryInfo;
  33. #endregion
  34. namespace Eq2VpkTool
  35. {
  36. /// <summary>
  37. /// Manages the image list used to represent folder and file icons on the TreeView and ListView controls.
  38. /// </summary>
  39. public class IconManager
  40. {
  41. #region Methods
  42. #region Constructors
  43. public IconManager(ImageList imageList)
  44. {
  45. this.imageList = imageList;
  46. // Create the directory icon in advance.
  47. imageList.Images.Add(GetIcon("directory", true, IconSize.Small));
  48. }
  49. #endregion
  50. /// <summary>
  51. /// Returns the index of the directory icon.
  52. /// </summary>
  53. /// <returns>Index in the image list of the directory icon.</returns>
  54. public int GetDirectoryImageIndex()
  55. {
  56. return imageList.Images.IndexOfKey("directory");
  57. }
  58. /// <summary>
  59. /// Returns the index of the icon that best represents the provided item.
  60. /// </summary>
  61. /// <param name="item">File or directory to get the icon from.</param>
  62. /// <returns>Index in the image list of the appropriate icon.</returns>
  63. public int GetImageIndex(Eq2FileSystemInfo item)
  64. {
  65. bool isDirectory = item is Eq2DirectoryInfo;
  66. string name;
  67. if (isDirectory)
  68. {
  69. // All directories share the same key on the image list.
  70. name = "directory";
  71. }
  72. else
  73. {
  74. Eq2FileInfo file = item as Eq2FileInfo;
  75. // Get the image list key from the file name. The extension will be used for this purpose.
  76. name = System.IO.Path.GetExtension(file.Name);
  77. }
  78. int imageIndex;
  79. lock (imageList)
  80. {
  81. if (!imageList.Images.ContainsKey(name))
  82. {
  83. // If the image list doesn't contain an icon for this item, get it from the OS and cache it.
  84. imageList.Images.Add(name, GetIcon(name, isDirectory, IconSize.Small));
  85. }
  86. imageIndex = imageList.Images.IndexOfKey(name);
  87. }
  88. return imageIndex;
  89. }
  90. #endregion
  91. // The following code is borrowed from http://www.sliver.com/dotnet/FileSystemIcons/
  92. // Slightly modified to not require physical files on disk.
  93. #region Icon querying code
  94. [StructLayout(LayoutKind.Sequential)]
  95. private struct SHFILEINFO
  96. {
  97. public IntPtr hIcon;
  98. public int iIcon;
  99. public int dwAttributes;
  100. public string szDisplayName;
  101. public string szTypeName;
  102. }
  103. private enum IconSize
  104. {
  105. Large,
  106. Small
  107. }
  108. private const uint SHGFI_LARGEICON = 0x0;
  109. private const uint SHGFI_SMALLICON = 0x1;
  110. private const uint SHGFI_USEFILEATTRIBUTES = 0x10;
  111. private const uint SHGFI_ICON = 0x100;
  112. private const uint SHGFI_SYSICONINDEX = 0x4000;
  113. private const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
  114. private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
  115. [DllImport("Shell32.dll")]
  116. private static extern int SHGetFileInfo(string path, uint fileAttributes, out SHFILEINFO psfi, uint fileInfo, uint flags);
  117. private static Icon GetIcon(string name, bool isDirectory, IconSize iconSize)
  118. {
  119. SHFILEINFO info = new SHFILEINFO();
  120. uint sizeFlags = iconSize == IconSize.Large ? SHGFI_LARGEICON : SHGFI_SMALLICON;
  121. uint attributes = isDirectory ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL;
  122. uint flags = SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_ICON | sizeFlags;
  123. int hTcdf = SHGetFileInfo(name, attributes, out info, (uint)Marshal.SizeOf(typeof(SHFILEINFO)), flags);
  124. return Icon.FromHandle(info.hIcon);
  125. }
  126. #endregion
  127. #region Properties
  128. public ImageList ImageList
  129. {
  130. get { lock (imageList) return imageList; }
  131. }
  132. #endregion
  133. #region Fields
  134. private ImageList imageList;
  135. #endregion
  136. }
  137. }