forked from ADEOSec/ADEORATShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiconmodule.bas
241 lines (205 loc) · 10.2 KB
/
iconmodule.bas
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
Attribute VB_Name = "iconmodule"
Option Explicit
Private Type Guid
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type PictDesc
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Type PWMFRect16
Left As Integer
Top As Integer
Right As Integer
Bottom As Integer
End Type
Private Type wmfPlaceableFileHeader
key As Long
hMf As Integer
BoundingBox As PWMFRect16
Inch As Integer
Reserved As Long
CheckSum As Integer
End Type
' GDI Functions
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal HDC As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PictDesc, RefIID As Guid, ByVal fPictureOwnsHandle As Long, iPic As IPicture) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal HDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal HDC As Long, ByVal nIndex As Long) As Long
Private Declare Function PatBlt Lib "gdi32" (ByVal HDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal dwRop As Long) As Long
Private Declare Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal HDC As Long, ByVal hObject As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal HDC As Long) As Long
' GDI+ functions
Private Declare Function GdipLoadImageFromFile Lib "gdiplus.dll" (ByVal Filename As Long, GpImage As Long) As Long
Private Declare Function GdiplusStartup Lib "gdiplus.dll" (Token As Long, gdipInput As GdiplusStartupInput, GdiplusStartupOutput As Long) As Long
Private Declare Function GdipCreateFromHDC Lib "gdiplus.dll" (ByVal HDC As Long, GpGraphics As Long) As Long
Private Declare Function GdipSetInterpolationMode Lib "gdiplus.dll" (ByVal Graphics As Long, ByVal InterMode As Long) As Long
Private Declare Function GdipDrawImageRectI Lib "gdiplus.dll" (ByVal Graphics As Long, ByVal Img As Long, ByVal X As Long, ByVal Y As Long, ByVal Width As Long, ByVal Height As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "gdiplus.dll" (ByVal Graphics As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus.dll" (ByVal Image As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "gdiplus.dll" (ByVal hBmp As Long, ByVal hPal As Long, GpBitmap As Long) As Long
Private Declare Function GdipGetImageWidth Lib "gdiplus.dll" (ByVal Image As Long, Width As Long) As Long
Private Declare Function GdipGetImageHeight Lib "gdiplus.dll" (ByVal Image As Long, Height As Long) As Long
Private Declare Function GdipCreateMetafileFromWmf Lib "gdiplus.dll" (ByVal hWmf As Long, ByVal deleteWmf As Long, WmfHeader As wmfPlaceableFileHeader, Metafile As Long) As Long
Private Declare Function GdipCreateMetafileFromEmf Lib "gdiplus.dll" (ByVal hEmf As Long, ByVal deleteEmf As Long, Metafile As Long) As Long
Private Declare Function GdipCreateBitmapFromHICON Lib "gdiplus.dll" (ByVal hIcon As Long, GpBitmap As Long) As Long
Private Declare Function GdipDrawImageRectRectI Lib "gdiplus.dll" (ByVal Graphics As Long, ByVal GpImage As Long, ByVal DstX As Long, ByVal DstY As Long, ByVal dstwidth As Long, ByVal dstheight As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal srcwidth As Long, ByVal srcheight As Long, ByVal srcUnit As Long, ByVal imageAttributes As Long, ByVal callback As Long, ByVal callbackData As Long) As Long
Private Declare Sub GdiplusShutdown Lib "gdiplus.dll" (ByVal Token As Long)
' GDI and GDI+ constants
Private Const PLANES = 14 ' Number of planes
Private Const BITSPIXEL = 12 ' Number of bits per pixel
Private Const PATCOPY = &HF00021 ' (DWORD) dest = pattern
Private Const PICTYPE_BITMAP = 1 ' Bitmap type
Private Const InterpolationModeHighQualityBicubic = 7
Private Const GDIP_WMF_PLACEABLEKEY = &H9AC6CDD7
Private Const UnitPixel = 2
' Initialises GDI Plus
Public Function InitGDIPlus() As Long
Dim Token As Long
Dim gdipInit As GdiplusStartupInput
gdipInit.GdiplusVersion = 1
GdiplusStartup Token, gdipInit, ByVal 0&
InitGDIPlus = Token
End Function
' Frees GDI Plus
Public Sub FreeGDIPlus(Token As Long)
GdiplusShutdown Token
End Sub
' Loads the picture (optionally resized)
Public Function LoadPictureGDIPlus(PicFile As String, Optional Width As Long = -1, Optional Height As Long = -1, Optional ByVal BackColor As Long = vbWhite, Optional RetainRatio As Boolean = False) As IPicture
Dim HDC As Long
Dim hBitmap As Long
Dim Img As Long
' Load the image
If GdipLoadImageFromFile(StrPtr(PicFile), Img) <> 0 Then
'// Err.Raise 999, "GDI+ Module", "Error loading picture " & PicFile
Exit Function
End If
' Calculate picture's width and height if not specified
If Width = -1 Or Height = -1 Then
GdipGetImageWidth Img, Width
GdipGetImageHeight Img, Height
End If
' Initialise the hDC
InitDC HDC, hBitmap, BackColor, Width, Height
' Resize the picture
gdipResize Img, HDC, Width, Height, RetainRatio
GdipDisposeImage Img
' Get the bitmap back
GetBitmap HDC, hBitmap
' Create the picture
Set LoadPictureGDIPlus = CreatePicture(hBitmap)
End Function
' Initialises the hDC to draw
Private Sub InitDC(HDC As Long, hBitmap As Long, BackColor As Long, Width As Long, Height As Long)
Dim hBrush As Long
' Create a memory DC and select a bitmap into it, fill it in with the backcolor
HDC = CreateCompatibleDC(ByVal 0&)
hBitmap = CreateBitmap(Width, Height, GetDeviceCaps(HDC, PLANES), GetDeviceCaps(HDC, BITSPIXEL), ByVal 0&)
hBitmap = SelectObject(HDC, hBitmap)
hBrush = CreateSolidBrush(BackColor)
hBrush = SelectObject(HDC, hBrush)
PatBlt HDC, 0, 0, Width, Height, PATCOPY
DeleteObject SelectObject(HDC, hBrush)
End Sub
' Resize the picture using GDI plus
Private Sub gdipResize(Img As Long, HDC As Long, Width As Long, Height As Long, Optional RetainRatio As Boolean = False)
Dim Graphics As Long ' Graphics Object Pointer
Dim OrWidth As Long ' Original Image Width
Dim OrHeight As Long ' Original Image Height
Dim OrRatio As Double ' Original Image Ratio
Dim DesRatio As Double ' Destination rect Ratio
Dim DestX As Long ' Destination image X
Dim DestY As Long ' Destination image Y
Dim DestWidth As Long ' Destination image Width
Dim DestHeight As Long ' Destination image Height
GdipCreateFromHDC HDC, Graphics
GdipSetInterpolationMode Graphics, InterpolationModeHighQualityBicubic
If RetainRatio Then
GdipGetImageWidth Img, OrWidth
GdipGetImageHeight Img, OrHeight
OrRatio = OrWidth / OrHeight
DesRatio = Width / Height
' Calculate destination coordinates
DestWidth = IIf(DesRatio < OrRatio, Width, Height * OrRatio)
DestHeight = IIf(DesRatio < OrRatio, Width / OrRatio, Height)
DestX = (Width - DestWidth) / 2
DestY = (Height - DestHeight) / 2
GdipDrawImageRectRectI Graphics, Img, DestX, DestY, DestWidth, DestHeight, 0, 0, OrWidth, OrHeight, UnitPixel, 0, 0, 0
Else
GdipDrawImageRectI Graphics, Img, 0, 0, Width, Height
End If
GdipDeleteGraphics Graphics
End Sub
' Replaces the old bitmap of the hDC, Returns the bitmap and Deletes the hDC
Private Sub GetBitmap(HDC As Long, hBitmap As Long)
hBitmap = SelectObject(HDC, hBitmap)
DeleteDC HDC
End Sub
' Creates a Picture Object from a handle to a bitmap
Private Function CreatePicture(hBitmap As Long) As IPicture
Dim IID_IDispatch As Guid
Dim Pic As PictDesc
Dim iPic As IPicture
' Fill in OLE IDispatch Interface ID
IID_IDispatch.Data1 = &H20400
IID_IDispatch.Data4(0) = &HC0
IID_IDispatch.Data4(7) = &H46
' Fill Pic with necessary parts
Pic.Size = Len(Pic) ' Length of structure
Pic.Type = PICTYPE_BITMAP ' Type of Picture (bitmap)
Pic.hBmp = hBitmap ' Handle to bitmap
' Create the picture
OleCreatePictureIndirect Pic, IID_IDispatch, True, iPic
Set CreatePicture = iPic
End Function
' Returns a resized version of the picture
Public Function Resize(Handle As Long, picType As PictureTypeConstants, Width As Long, Height As Long, Optional BackColor As Long = vbWhite, Optional RetainRatio As Boolean = False) As IPicture
Dim Img As Long
Dim HDC As Long
Dim hBitmap As Long
Dim WmfHeader As wmfPlaceableFileHeader
' Determine pictyre type
Select Case picType
Case vbPicTypeBitmap
GdipCreateBitmapFromHBITMAP Handle, ByVal 0&, Img
Case vbPicTypeMetafile
FillInWmfHeader WmfHeader, Width, Height
GdipCreateMetafileFromWmf Handle, False, WmfHeader, Img
Case vbPicTypeEMetafile
GdipCreateMetafileFromEmf Handle, False, Img
Case vbPicTypeIcon
' Does not return a valid Image object
GdipCreateBitmapFromHICON Handle, Img
End Select
' Continue with resizing only if we have a valid image object
If Img Then
InitDC HDC, hBitmap, BackColor, Width, Height
gdipResize Img, HDC, Width, Height, RetainRatio
GdipDisposeImage Img
GetBitmap HDC, hBitmap
Set Resize = CreatePicture(hBitmap)
End If
End Function
' Fills in the wmfPlacable header
Private Sub FillInWmfHeader(WmfHeader As wmfPlaceableFileHeader, Width As Long, Height As Long)
WmfHeader.BoundingBox.Right = Width
WmfHeader.BoundingBox.Bottom = Height
WmfHeader.Inch = 1440
WmfHeader.key = GDIP_WMF_PLACEABLEKEY
End Sub