-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiveThumb.ahk
320 lines (273 loc) · 13 KB
/
LiveThumb.ahk
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
; ----------------------------------------------------------------------------------------------------------------------
; Name .........: LiveThumb class library
; Description ..: Windows live thumbnails implementation (requires AERO to be enabled).
; AHK Version ..: AHK_L 1.1.30.01 x32/64 ANSI/Unicode
; Author .......: cyruz - http://ciroprincipe.info
; Thanks .......: maul-esel - https://github.com/maul-esel/AeroThumbnail
; License ......: WTFPL - http://www.wtfpl.net/txt/copying/
; Changelog ....: Feb. 21, 2019 - v0.1.0 - First version.
; ..............: Mar. 04, 2019 - v0.1.1 - Added active properties management and propertes getters.
; ..............: Jul. 28, 2019 - v0.1.2 - Added "Discard" method. Fixed LoadLibrary return type and a wrong behavior
; ..............: when returning false from the constructor (thanks Helgef).
; ..............: Sep. 21, 2019 - v0.1.3 - Fixed potential issue with HRESULT return code. Used internal memory
; ..............: management instead of LocalAlloc.
; ..............: Sep. 21, 2019 - v0.1.4 - Fixed Object.SetCapacity not zero-filling allocated memory.
; ..............: Nov. 15, 2022 - v0.1.5 - Fixed DWM_THUMB_PROPERTIES structure offsets (thanks swagfag).
; Remarks ......: The class registers a thumbnail and waits for properties update. When all the desired properties have
; ..............: been updated, the "Update()" method should be called to submit the properties. Getting a property
; ..............: when an update has not been performed before, will result in the string "NOT UPDATED" to be returned.
; ..............: Due to some internal unknown reason, the instantiated object must be in the global namespace to work.
; Info .........: Implements the following functions and structures from Win32 API.
; ..............: "DwmRegisterThumbnail" Win32 function:
; ..............: https://docs.microsoft.com/en-us/windows/desktop/api/dwmapi/nf-dwmapi-dwmregisterthumbnail
; ..............: "SIZE" Win32 structure:
; ..............: https://docs.microsoft.com/en-us/previous-versions//dd145106(v=vs.85)
; ..............: "DwmQueryThumbnailSourceSize" Win32 function:
; ..............: https://docs.microsoft.com/en-us/windows/desktop/api/dwmapi/nf-dwmapi-dwmquerythumbnailsourcesize
; ..............: "DWM_THUMB_PROPERTIES" Win32 structure:
; ..............: https://docs.microsoft.com/en-us/windows/desktop/api/dwmapi/ns-dwmapi-_dwm_thumbnail_properties
; ..............: "DWM_TNP" Constants for the dwFlags field:
; ..............: https://docs.microsoft.com/en-us/windows/desktop/dwm/dwm-tnp-constants
; ..............: "DwmUpdateThumbnailProperties" Win32 function:
; ..............: https://docs.microsoft.com/en-us/windows/desktop/api/dwmapi/nf-dwmapi-dwmupdatethumbnailproperties
; ..............: "DwmUnregisterThumbnail" Win32 function:
; ..............: https://docs.microsoft.com/en-us/windows/desktop/api/dwmapi/nf-dwmapi-dwmunregisterthumbnail
; ----------------------------------------------------------------------------------------------------------------------
/*
* How to use:
-------------
Instantiate the object:
thumb := New LiveThumb(hSource, hDestination)
Modify its properties:
thumb.Source := [0, 0, 300, 300]
thumb.Destination := [0, 0, 500, 500]
thumb.Visible := True
Update the properties:
thumb.Update()
Get a property:
aProp := thumb.Visible
* Example:
----------
#SingleInstance Force
#Include <LiveThumb>
Run, mspaint.exe,,, nPid
WinWaitActive, ahk_pid %nPid%
WinGet, hPaint, ID, ahk_pid %nPid%
WinGetPos,,, nW, nH, ahk_pid %nPid%
Gui, +HwndhGui
hLT := new LiveThumb(hPaint, hGui)
hLT.Source := [0, 0, nW, nH]
hLT.Destination := [0, 0, nW, nH]
hLT.Visible := True
hLT.Update()
Gui, Show, w%nW% h%nH%
*/
; Name .........: LiveThumb - PUBLIC CLASS
; Description ..: Manages thumbnails creation and destruction.
Class LiveThumb
{
Static DLL_MODULE := 0
, OBJ_COUNTER := 0
, DTP_SIZE := 48
, DTP_DWFLAGS := { "Destination" : 0x0001
, "Source" : 0x0002
, "Opacity" : 0x0004
, "Visible" : 0x0008
, "SourceClientAreaOnly" : 0x0010 }
, DTP_OFFSETS := { "dwFlags" : 0
, "Destination" : 4
, "Source" : 20
, "Opacity" : 36
, "Visible" : 37
, "SourceClientAreaOnly" : 41 }
; Name .........: __New - PRIVATE CONSTRUCTOR
; Description ..: Initialize the object, registering a new thumbnail and allocating the memory.
; Parameters ...: hSource = Handle to the window to be previewed.
; ..............: hDest = Handle to the window containing the live preview.
; Return .......: LiveThumb object on success - False on error.
__New( hSource, hDest )
{
; Load the library on first run.
If ( !LiveThumb.DLL_MODULE )
LiveThumb.DLL_MODULE := DllCall("LoadLibrary", "Str","dwmapi.dll", "Ptr")
LiveThumb.OBJ_COUNTER += 1
; Register a thumbnail to get an ID.
If ( DllCall( "dwmapi.dll\DwmRegisterThumbnail"
, "Ptr", hDest
, "Ptr", hSource
, "Ptr*", phThumb
, "UPtr" ) )
Return False
this.THUMB_ID := phThumb
this.THUMB_UPDATED := False
; We define 2 portions of memory used for properties update and active properties tracking.
this.SetCapacity("THUMB_UPD_PROP", LiveThumb.DTP_SIZE)
this.SetCapacity("THUMB_ACT_PROP", LiveThumb.DTP_SIZE)
this.THUMB_UPD_PROP_PTR := this.GetAddress("THUMB_UPD_PROP")
this.THUMB_ACT_PROP_PTR := this.GetAddress("THUMB_ACT_PROP")
; Object.SetCapacity doesn't zero-fill the allocated memory so we call the Discard method.
this.Discard( )
Return this
}
; Name .........: __Delete - PRIVATE DESTRUCTOR
; Description ..: Unregister the thumbnail and deallocate memory.
__Delete( )
{
; Unregister thumbnail ID.
If ( this.THUMB_ID )
DllCall( "dwmapi.dll\DwmUnregisterThumbnail"
, "Ptr", this.THUMB_ID )
; If it's last instantiated object, free the library.
If ( LiveThumb.DLL_MODULE && !(LiveThumb.OBJ_COUNTER -= 1) )
{
DllCall("FreeLibrary", "Ptr",LiveThumb.DLL_MODULE)
LiveThumb.DLL_MODULE := 0
}
}
; Name .........: __Get - PRIVATE META FUNCTION
; Description ..: Return the string "NOT UPDATED" if properties have not been updated on each property get request.
__Get( aName )
{
If ( LiveThumb.DTP_DWFLAGS[aName] && !this.THUMB_UPDATED )
Return "NOT UPDATED"
}
; Name .........: __Set - PRIVATE META FUNCTION
; Description ..: Update the dwFlags field of the structure on each property update.
__Set( aName, aVal )
{
If ( LiveThumb.DTP_DWFLAGS[aName] )
{
NumPut( NumGet( this.THUMB_UPD_PROP_PTR+0
, LiveThumb.DTP_OFFSETS.dwFlags
, "UInt" ) | LiveThumb.DTP_DWFLAGS[aName]
, this.THUMB_UPD_PROP_PTR+0
, LiveThumb.DTP_OFFSETS.dwFlags
, "UInt" )
this.THUMB_PENDING_UPDATE := True
}
}
; Name .........: QuerySourceSize - PUBLIC METHOD
; Description ..: Return the size (width/height) of the source thumbnail.
; Return .......: Array with width and height values - False on error.
QuerySourceSize( )
{
VarSetCapacity(SIZE, 8, 0)
If ( DllCall( "dwmapi.dll\DwmQueryThumbnailSourceSize"
, "Ptr", this.THUMB_ID
, "Ptr", &SIZE
, "UPtr" ) )
Return False, VarSetCapacity(SIZE, 0)
Return [NumGet(SIZE, 0, "Int"), NumGet(SIZE, 4, "Int")], VarSetCapacity(SIZE, 0)
}
; Name .........: Update - PUBLIC METHOD
; Description ..: Update thumbnail properties and zero fill the dwFlags memory to be ready for next update.
; Return .......: True on success - False on error.
Update( )
{
; If no update is pending, return false.
If ( !this.THUMB_PENDING_UPDATE )
Return False
; Update properties.
If ( DllCall( "dwmapi.dll\DwmUpdateThumbnailProperties"
, "Ptr", this.THUMB_ID
, "Ptr", this.THUMB_UPD_PROP_PTR
, "UPtr" ) )
Return False
; Flag as updated and copy memory so that we can use this portion to track active properties with getters.
this.THUMB_UPDATED := True
DllCall( "NtDll.dll\RtlCopyMemory"
, "Ptr", this.THUMB_ACT_PROP_PTR
, "Ptr", this.THUMB_UPD_PROP_PTR
, "UInt", LiveThumb.DTP_SIZE )
; Use the "Discard" method to reset dwFlags and return.
this.Discard( )
Return True
}
; Name .........: Discard - PUBLIC METHOD
; Description ..: Discard set properties, resetting dwFlags memory.
Discard( )
{
; Zero-fill dwFlags memory in the properties update memory portion.
NumPut(0, this.THUMB_UPD_PROP_PTR+0, LiveThumb.DTP_OFFSETS.dwFlags, "UInt")
this.THUMB_PENDING_UPDATE := False
}
; Name .........: Destination - PUBLIC PROPERTY
; Description ..: The area in the destination window where the thumbnail will be rendered.
; Value ........: Array with 4 client related coordinates [ left, top, right, bottom ].
; Remarks ......: "Destination" property is of "RECT" type (16 bytes structure) and starts from offset 4.
Destination[] {
Get {
arrRet := []
Loop 4
arrRet.Push(NumGet(this.THUMB_ACT_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Destination * A_Index, "Int"))
Return arrRet
}
Set {
Loop 4
NumPut(value[A_Index], this.THUMB_UPD_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Destination * A_Index, "Int")
}
}
; Name .........: Source - PUBLIC PROPERTY
; Description ..: The region of the source window to use as the thumbnail. Default is the entire window.
; Value ........: Array with 4 client related coordinates [ left, top, right, bottom ].
; Remarks ......: "Source" property is of "RECT" type (16 bytes structure) and starts from offset 20.
Source[] {
Get {
arrRet := []
Loop 4
arrRet.Push(NumGet(this.THUMB_ACT_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Source + 4*(A_Index-1), "Int"))
Return arrRet
}
Set {
Loop 4
NumPut(value[A_Index], this.THUMB_UPD_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Source + 4*(A_Index-1), "Int")
}
}
; Name .........: Opacity - PUBLIC PROPERTY
; Description ..: The opacity with which to render the thumbnail. 0: transparent - 255: opaque. Default is 255.
; Value ........: Integer value from 0 to 255.
; Remarks ......: "Opacity" property is of "BYTE" type (1 byte + 3 padding) and starts from offset 36.
Opacity[] {
Get {
Return NumGet(this.THUMB_ACT_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Opacity, "UChar")
}
Set {
NumPut(value, this.THUMB_UPD_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Opacity, "UChar")
}
}
; Name .........: Visible - PUBLIC PROPERTY
; Description ..: True to make the thumbnail visible, otherwise False. Default is False.
; Value ........: Boolean True/False or integer 1/0 value.
; Remarks ......: "Visible" property is of "BOOL" type (4 bytes) and starts from offset 40.
Visible[] {
Get {
Return NumGet(this.THUMB_ACT_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Visible, "Int")
}
Set {
NumPut(value, this.THUMB_UPD_PROP_PTR+0, LiveThumb.DTP_OFFSETS.Visible, "Int")
}
}
; Name .........: SourceClientAreaOnly - PUBLIC PROPERTY
; Description ..: True to use only the thumbnail source's client area, otherwise False. Default is False.
; Value ........: Boolean True/False or integer 1/0 value.
; Remarks ......: "SourceClientAreaOnly" property is of "BOOL" type (4 bytes) and starts from offset 44.
SourceClientAreaOnly[] {
Get {
Return NumGet(this.THUMB_ACT_PROP_PTR+0, LiveThumb.DTP_OFFSETS.SourceClientAreaOnly, "Int")
}
Set {
NumPut(value, this.THUMB_UPD_PROP_PTR+0, LiveThumb.DTP_OFFSETS.SourceClientAreaOnly, "Int")
}
}
}
Run, mspaint.exe,,, nPid
WinWaitActive, ahk_pid %nPid%
WinGet, hPaint, ID, ahk_pid %nPid%
WinGetPos,,, nW, nH, ahk_pid %nPid%
Gui, +HwndhGui
hLT := new LiveThumb(hPaint, hGui)
hLT.Source := [0, 0, nW, nH]
hLT.Destination := [0, 0, nW, nH]
hLT.Visible := True
hLT.Update()
Gui, Show, w%nW% h%nH%