-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemIconsImageList.cs
178 lines (157 loc) · 5.4 KB
/
SystemIconsImageList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
// https://www.codeproject.com/Articles/25534/A-class-to-load-and-use-file-associated-icons
namespace TrayFolder
{
public class SystemIconsImageList : IDisposable
{
#region Win32 declarations
private const uint SHGFI_ICON = 0x100;
private const uint SHGFI_LARGEICON = 0x0;
private const uint SHGFI_SMALLICON = 0x1;
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo( string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags );
#endregion
#region Fields
private ImageList _smallImageList = new ImageList();
private ImageList _largeImageList = new ImageList();
private bool _disposed = false;
#endregion
#region Properties
/// <summary>
/// Gets System.Windows.Forms.ImageList with small icons in. Assign this property to SmallImageList of ListView, TreeView etc.
/// </summary>
public ImageList SmallIconsImageList
{
get { return _smallImageList; }
}
/// <summary>
/// Gets System.Windows.Forms.ImageList with large icons in. Assign this property to LargeImageList of ListView, TreeView etc.
/// </summary>
public ImageList LargeIconsImageList
{
get { return _largeImageList; }
}
/// <summary>
/// Gets number of icons were loaded
/// </summary>
public int Count
{
get { return _smallImageList.Images.Count; }
}
#endregion
#region Constructor/Destructor
/// <summary>
/// Default constructor
/// </summary>
public SystemIconsImageList()
: base()
{
_smallImageList.ColorDepth = ColorDepth.Depth32Bit;
_smallImageList.ImageSize = SystemInformation.SmallIconSize;
_largeImageList.ColorDepth = ColorDepth.Depth32Bit;
_largeImageList.ImageSize = SystemInformation.IconSize;
}
private void CleanUp( bool disposing )
{
if(!this._disposed)
{
if(disposing)
{
_smallImageList.Dispose();
_largeImageList.Dispose();
}
}
_disposed = true;
}
/// <summary>
/// Performs resource cleaning
/// </summary>
public void Dispose()
{
CleanUp(true);
GC.SuppressFinalize(this);
}
~SystemIconsImageList()
{
CleanUp(false);
}
#endregion
#region Public Methods
/// <summary>
/// Returns index of an icon based on FileName. Note: File should exists!
/// </summary>
/// <param name="FileName">Name of an existing File or Directory</param>
/// <returns>Index of an Icon</returns>
public int GetIconIndex( string FileName )
{
SHFILEINFO shinfo = new SHFILEINFO();
FileInfo info = new FileInfo(FileName);
string ext = info.Extension;
if(String.IsNullOrEmpty(ext))
{
if((info.Attributes & FileAttributes.Directory) != 0)
ext = "5EEB255733234c4dBECF9A128E896A1E"; // for directories
else
ext = "F9EB930C78D2477c80A51945D505E9C4"; // for files without extension
}
else
if(ext.Equals(".exe", StringComparison.InvariantCultureIgnoreCase) ||
ext.Equals(".lnk", StringComparison.InvariantCultureIgnoreCase))
ext = info.Name;
if(_smallImageList.Images.ContainsKey(ext))
{
return _smallImageList.Images.IndexOfKey(ext);
}
else
{
SHGetFileInfo(FileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_SMALLICON);
Icon smallIcon;
try
{
smallIcon = Icon.FromHandle(shinfo.hIcon);
}
catch(ArgumentException ex)
{
throw new ArgumentException(String.Format("File \"{0}\" doesn not exist!", FileName), ex);
}
_smallImageList.Images.Add(ext, smallIcon);
SHGetFileInfo(FileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), SHGFI_ICON | SHGFI_LARGEICON);
Icon largeIcon = Icon.FromHandle(shinfo.hIcon);
_largeImageList.Images.Add(ext, largeIcon);
return _smallImageList.Images.Count - 1;
}
}
#endregion
}
}