-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShellIcons.cs
More file actions
142 lines (122 loc) · 5.02 KB
/
ShellIcons.cs
File metadata and controls
142 lines (122 loc) · 5.02 KB
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
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace AGS6_Dashboard
{
// -----------------------------------------------------------------------
// <copyright file="ShellIcon.cs" company="Mauricio DIAZ ORLICH (madd0@madd0.com)">
// Distributed under Microsoft Public License (MS-PL).
// http://www.opensource.org/licenses/MS-PL
// </copyright>
// -----------------------------------------------------------------------
/// <summary>
/// Get a small or large Icon with an easy C# function call
/// that returns a 32x32 or 16x16 System.Drawing.Icon depending on which function you call
/// either GetSmallIcon(string fileName) or GetLargeIcon(string fileName)
/// </summary>
public static class ShellIcon
{
#region Interop constants
private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
private const uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
#endregion
#region Interop data types
[StructLayout(LayoutKind.Sequential)]
private 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;
}
[Flags]
private enum SHGFI : int
{
/// <summary>get icon</summary>
Icon = 0x000000100,
/// <summary>get display name</summary>
DisplayName = 0x000000200,
/// <summary>get type name</summary>
TypeName = 0x000000400,
/// <summary>get attributes</summary>
Attributes = 0x000000800,
/// <summary>get icon location</summary>
IconLocation = 0x000001000,
/// <summary>return exe type</summary>
ExeType = 0x000002000,
/// <summary>get system icon index</summary>
SysIconIndex = 0x000004000,
/// <summary>put a link overlay on icon</summary>
LinkOverlay = 0x000008000,
/// <summary>show icon in selected state</summary>
Selected = 0x000010000,
/// <summary>get only specified attributes</summary>
Attr_Specified = 0x000020000,
/// <summary>get large icon</summary>
LargeIcon = 0x000000000,
/// <summary>get small icon</summary>
SmallIcon = 0x000000001,
/// <summary>get open icon</summary>
OpenIcon = 0x000000002,
/// <summary>get shell size icon</summary>
ShellIconSize = 0x000000004,
/// <summary>pszPath is a pidl</summary>
PIDL = 0x000000008,
/// <summary>use passed dwFileAttribute</summary>
UseFileAttributes = 0x000000010,
/// <summary>apply the appropriate overlays</summary>
AddOverlays = 0x000000020,
/// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary>
OverlayIndex = 0x000000040,
}
#endregion
private class Win32
{
[DllImport("shell32.dll")]
public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);
[DllImport("User32.dll")]
public static extern int DestroyIcon(IntPtr hIcon);
}
public static Icon GetSmallFolderIcon()
{
return GetIcon("folder", SHGFI.SmallIcon | SHGFI.UseFileAttributes, true);
}
public static Icon GetLargeFolderIcon()
{
return GetIcon("folder", SHGFI.LargeIcon | SHGFI.UseFileAttributes, true);
}
public static Icon GetSmallIcon(string fileName)
{
return GetIcon(fileName, SHGFI.SmallIcon);
}
public static Icon GetLargeIcon(string fileName)
{
return GetIcon(fileName, SHGFI.LargeIcon);
}
public static Icon GetSmallIconFromExtension(string extension)
{
return GetIcon(extension, SHGFI.SmallIcon | SHGFI.UseFileAttributes);
}
public static Icon GetLargeIconFromExtension(string extension)
{
return GetIcon(extension, SHGFI.LargeIcon | SHGFI.UseFileAttributes);
}
private static Icon GetIcon(string fileName, SHGFI flags, bool isFolder = false)
{
SHFILEINFO shinfo = new SHFILEINFO();
IntPtr hImgSmall = Win32.SHGetFileInfo(fileName, isFolder ? FILE_ATTRIBUTE_DIRECTORY : FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), (uint)(SHGFI.Icon | flags));
Icon icon = (Icon)Icon.FromHandle(shinfo.hIcon).Clone();
Win32.DestroyIcon(shinfo.hIcon);
return icon;
}
}
}