-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathIconHelper.bas
More file actions
199 lines (119 loc) · 5.9 KB
/
IconHelper.bas
File metadata and controls
199 lines (119 loc) · 5.9 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
Attribute VB_Name = "IconHelper"
Option Explicit
Private Const IID_IImageList As String = "{46EB5926-582E-4017-9FDF-E8998DAA0950}"
Private Const IID_IImageList2 As String = "{192B9D83-50FC-457B-90A0-2B82A8B5DAE1}"
Private Const E_INVALIDARG As Long = &H80070057
Private Const ILD_NORMAL As Long = 0
Private Const ILD_TRANSPARENT = &H1 'display transparent
Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_EXETYPE = &H2000
Private Const SHGFI_SYSICONINDEX = &H4000 'system icon index
Private Const SHGFI_LARGEICON = &H0 'large icon
Private Const SHGFI_SMALLICON = &H1 'small icon
Private Const SHGFI_EXTRALARGE = &H2
Private Const SHGFI_SHELLICONSIZE = &H4
Private Const SHGFI_TYPENAME = &H400
Private Const BASIC_SHGFI_FLAGS = SHGFI_TYPENAME Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_DISPLAYNAME Or SHGFI_EXETYPE
Private Enum SHIL_FLAG
SHIL_LARGE = &H0 ' The image size is normally 32x32 pixels. However, if the Use large icons option is selected from the Effects section of the Appearance tab in Display Properties, the image is 48x48 pixels.
SHIL_SMALL = &H1 ' These images are the Shell standard small icon size of 16x16, but the size can be customized by the user.
SHIL_EXTRALARGE = &H2 ' These images are the Shell standard extra-large icon size. This is typically 48x48, but the size can be customized by the user.
SHIL_SYSSMALL = &H3 ' These images are the size specified by GetSystemMetrics called with SM_CXSMICON and GetSystemMetrics called with SM_CYSMICON.
SHIL_JUMBO = &H4 ' Windows Vista and later. The image is normally 256x256 pixels.
End Enum
Private Function DrawIconToHDC(aFile As String, theHDC As Long)
Dim aImgList As Long
Dim SFI As SHFILEINFO
SHGetFileInfo aFile, FILE_ATTRIBUTE_NORMAL, SFI, Len(SFI), SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_TYPENAME Or SHGFI_DISPLAYNAME
aImgList = GetImageListSH(SHIL_JUMBO)
ImageList_Draw aImgList, SFI.iIcon, theHDC, 0, 0, ILD_NORMAL
End Function
Public Function IconIs48(aFile As String) As Boolean
Dim newHDC As New cMemDC
Dim pixelA As Long
Dim pixelB As Long
newHDC.Height = 256
newHDC.Width = 256
DrawIconToHDC aFile, newHDC.hdc
IconIs48 = True
For pixelA = 48 To 255
For pixelB = 48 To 255
If GetPixel(newHDC.hdc, pixelA, pixelB) <> 0 Then
IconIs48 = False
Exit Function
End If
Next
Next
End Function
Private Function GetImageListSH(shFlag As SHIL_FLAG) As Long
Dim lResult As Long
Dim Guid(0 To 3) As Long
Dim himl As IUnknown
If Not IIDFromString(StrPtr(IID_IImageList), Guid(0)) = 0 Then
Exit Function
End If
lResult = SHGetImageListXP(CLng(shFlag), Guid(0), ByVal VarPtr(himl))
GetImageListSH = ObjPtr(himl)
End Function
Public Function GetIconFromHwnd(hWnd As Long) As Long
Call SendMessageTimeout(hWnd, WM_GETICON, ICON_BIG, 0, 0, 100, GetIconFromHwnd)
If Not CBool(GetIconFromHwnd) Then GetIconFromHwnd = GetClassLong(hWnd, GCL_HICON)
If Not CBool(GetIconFromHwnd) Then Call SendMessageTimeout(hWnd, WM_GETICON, 1, 0, 0, 100, GetIconFromHwnd)
If Not CBool(GetIconFromHwnd) Then GetIconFromHwnd = GetClassLong(hWnd, GCL_HICON)
If Not CBool(GetIconFromHwnd) Then Call SendMessageTimeout(hWnd, WM_QUERYDRAGICON, 0, 0, 0, 100, GetIconFromHwnd)
End Function
Public Function GetSmallApplicationIcon(strExePath As String) As Long
Dim shinfo As SHFILEINFO
Dim hImgSmall As Long
Dim win64Token As Win64FSToken
If Is64bit Then
If (InStr(LCase(strExePath), LCase(Environ("windir"))) > 0) Then
Set win64Token = New Win64FSToken
End If
End If
'get the system icon associated with that file
hImgSmall = WinAPIHelper.SHGetFileInfo(strExePath, 0&, shinfo, Len(shinfo), SHGFI_ICON Or SHGFI_SMALLICON Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_TYPENAME Or SHGFI_DISPLAYNAME)
GetSmallApplicationIcon = shinfo.hIcon
If Not win64Token Is Nothing Then
win64Token.EnableFS
End If
End Function
Function CreateSmallAlphaIcon(szPath As String) As AlphaIcon
Dim SmallIcon As Long
Dim FileInfo As SHFILEINFO
Dim newAlphaIcon As AlphaIcon
SmallIcon = SHGetFileInfo(szPath, 0&, FileInfo, Len(FileInfo), SHGFI_SMALLICON Or SHGFI_ICON)
Set newAlphaIcon = New AlphaIcon
newAlphaIcon.CreateFromHICON FileInfo.hIcon
DestroyIcon FileInfo.hIcon
Set CreateSmallAlphaIcon = newAlphaIcon
End Function
Public Function GetExtraLargeApplicationIcon(szPath As String) As Long
Dim aImgList As Long
Dim SFI As SHFILEINFO
Dim hIcon As Long
SHGetFileInfo szPath, FILE_ATTRIBUTE_NORMAL, SFI, Len(SFI), SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_TYPENAME Or SHGFI_DISPLAYNAME
If Not IconIs48(szPath) Then
aImgList = GetImageListSH(SHIL_JUMBO)
Else
aImgList = GetImageListSH(SHIL_EXTRALARGE)
End If
hIcon = ImageList_GetIcon(aImgList, SFI.iIcon, ILD_NORMAL)
GetExtraLargeApplicationIcon = hIcon
End Function
Public Function GetApplicationIcon(strExePath As String) As Long
Dim shinfo As SHFILEINFO
Dim hImgSmall As Long
Dim win64Token As Win64FSToken
If Is64bit Then
If (InStr(LCase(strExePath), LCase(Environ("windir"))) > 0) Then
Set win64Token = New Win64FSToken
End If
End If
'get the system icon associated with that file
hImgSmall = WinAPIHelper.SHGetFileInfo(strExePath, 0&, shinfo, Len(shinfo), SHGFI_ICON Or SHGFI_LARGEICON Or SHGFI_SHELLICONSIZE Or SHGFI_SYSICONINDEX Or SHGFI_TYPENAME Or SHGFI_DISPLAYNAME)
GetApplicationIcon = shinfo.hIcon
If Not win64Token Is Nothing Then
win64Token.EnableFS
End If
End Function