-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_ImagePut.ahk
1303 lines (1059 loc) · 55.8 KB
/
class_ImagePut.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Script: ImagePut.ahk
; Author: iseahound
; License: MIT License
; Version: 2020-05-22
; Release: 2020-05-26
; ImagePut - Puts an image from anywhere to anywhere.
; This is a simple functor designed to be intuitive.
; I hope people find this reference library useful.
; Puts the image into a file format and returns a base64 encoded string.
; extension - File Encoding | string -> bmp, gif, jpg, png, tiff
; quality - JPEG Quality Level | integer -> 0 - 100
ImagePutBase64(ByRef image, extension := "", quality := "") {
return ImagePut("base64", image,,, extension, quality)
}
; Puts the image into a GDI+ Bitmap and returns a pointer.
ImagePutBitmap(ByRef image) {
return ImagePut("bitmap", image)
}
; Puts the image into a GDI+ Bitmap and returns a buffer object with GDI+ scope.
ImagePutBuffer(ByRef image) {
return ImagePut("buffer", image)
}
; Puts the image onto the clipboard and returns an empty string.
ImagePutClipboard(ByRef image) {
return ImagePut("clipboard", image)
}
; Puts the image as the cursor and returns the string "A_Cursor".
; xHotspot - X Click Point | pixel -> 0 - width
; yHotspot - Y Click Point | pixel -> 0 - height
ImagePutCursor(ByRef image, xHotspot := "", yHotspot := "") {
return ImagePut("cursor", image,,, xHotspot, yHotspot)
}
; Puts the image behind the desktop icons and returns the string "desktop".
; scale - Scale Factor | real -> A_ScreenHeight / height.
ImagePutDesktop(ByRef image, scale := 1) {
return ImagePut("desktop", image,, scale)
}
; Puts the image into a file and returns the file name.
; filename - File Extension | string -> *.bmp, *.gif, *.jpg, *.png, *.tiff
; quality - JPEG Quality Level | integer -> 0 - 100
ImagePutFile(ByRef image, filename := "", quality := "") {
return ImagePut("file", image,,, filename, quality)
}
; Puts the image into a device independent bitmap and returns a handle.
; quality - JPEG Quality Level | integer -> 0 - 100
; alpha - Alpha Replacement Color | RGB -> 0xFFFFFF
ImagePutHBitmap(ByRef image, alpha := "") {
return ImagePut("hBitmap", image,,, alpha)
}
; Puts the image on the shared screen device context and returns an array of coordinates.
; screenshot - Screen Coordinates | array -> [x,y,w,h] or [0,0]
; alpha - Alpha Replacement Color | RGB -> 0xFFFFFF
ImagePutScreenshot(ByRef image, screenshot := "", alpha := "") {
return ImagePut("screenshot", image,,, screenshot, alpha)
}
; Puts the image as the desktop wallpaper and returns the string "wallpaper".
ImagePutWallpaper(ByRef image) {
return ImagePut("wallpaper", image)
}
; ImagePut() - Puts an image from anywhere to anywhere.
; cotype - Output Type | string -> Case Insensitive. Read documentation.
; image - Input Image | image -> Anything. Refer to ImageType().
; crop - Crop Coordinates | array -> [x,y,w,h] could be negative or percent.
; scale - Scale Factor | real -> 2.0
; terms* - Additional Parameters | variadic -> Extra parameters found in toCotype().
ImagePut(cotype, ByRef image, crop := "", scale := "", terms*) {
return ImagePut.call(cotype, image, crop, scale, terms*)
}
class ImagePut {
call(cotype, ByRef image, crop := "", scale := "", terms*) {
this.gdiplusStartup()
; Take a guess as to what the image might be. (>90% accuracy!)
try type := this.DontVerifyImageType(image)
catch
type := this.ImageType(image)
; Qualify additional parameters for correctness.
_crop := crop.1 ~= "^-?\d+(\.\d*)?%?$" && crop.2 ~= "^-?\d+(\.\d*)?%?$"
&& crop.3 ~= "^-?\d+(\.\d*)?%?$" && crop.4 ~= "^-?\d+(\.\d*)?%?$"
_scale := scale != 1 && scale ~= "^\d+(\.\d+)?$"
; Make a copy of the image as a pBitmap.
pBitmap := this.toBitmap(type, image)
; Crop the image.
if (_crop) {
pBitmap2 := this.BitmapCrop(pBitmap, crop)
DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
pBitmap := pBitmap2
}
; Scale the image.
if (_scale) {
pBitmap2 := this.BitmapScale(pBitmap, scale)
DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
pBitmap := pBitmap2
}
; Put the pBitmap to wherever the cotype specifies.
coimage := this.toCotype(cotype, pBitmap, terms*)
; Clean up the pBitmap copy. Export raw pointers if requested.
if !(cotype = "bitmap" || cotype = "buffer")
DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
this.gdiplusShutdown(cotype)
return coimage
}
; Types | Example | Explicit | Inferred | Input | Output |
; | | Don'tVerify | ImageType | toBitmap | toCotype |
; clipboard | ClipboardAll | yes | yes | yes | yes | - no transparency
; object | object.Bitmap() | yes | yes | yes | |
; buffer | bitmap.pBitmap | yes | yes | yes | yes |
; screenshot | [x,y,w,h] | yes | yes | yes | |
; desktop | "desktop" | yes | yes | no | yes |
; wallpaper | "wallpaper" | yes | yes | yes | yes |
; cursor | A_Cursor | yes | yes | yes | yes |
; url | https:// | yes | yes | yes | |
; file | picture.bmp | yes | yes | yes | yes |
; bitmap | some number | yes | yes | yes | yes |
; hBitmap | some number | yes | yes | yes | yes |
; monitor | 0 or # < 10 | yes | yes | | |
; hwnd | 0x | yes | yes | yes | |
; window | A | yes | yes | yes | |
; base64 | base64 data | yes | yes | yes | yes |
; sprite | file or url | yes | no | yes | no |
; icon | | | | | |
; printer | | | | | |
; findtext | | | | | |
; thumbnail | | ? | | | | DwmRegisterThumbnail
; video | | | | | |
; formdata | | | | | |
; stream | | | | | |
; randomaccessstream | pointer | | | | |
DontVerifyImageType(ByRef image) {
if !IsObject(image)
throw Exception("Must be an object.")
; Check for image type declarations.
; Assumes that the user is telling the truth.
if ObjHasKey(image, "clipboard") {
image := image.clipboard
return "clipboard"
}
if ObjHasKey(image, "object") {
image := image.object
return "object"
}
if ObjHasKey(image, "buffer") {
image := image.buffer
return "buffer"
}
if ObjHasKey(image, "screenshot") {
image := image.screenshot
return "screenshot"
}
if ObjHasKey(image, "desktop") {
image := image.desktop
return "desktop"
}
if ObjHasKey(image, "wallpaper") {
image := image.wallpaper
return "wallpaper"
}
if ObjHasKey(image, "cursor") {
image := image.cursor
return "cursor"
}
if ObjHasKey(image, "url") {
image := image.url
return "url"
}
if ObjHasKey(image, "file") {
image := image.file
return "file"
}
if ObjHasKey(image, "bitmap") {
image := image.bitmap
return "bitmap"
}
if ObjHasKey(image, "hBitmap") {
image := image.hBitmap
return "hBitmap"
}
if ObjHasKey(image, "monitor") {
image := image.monitor
return "monitor"
}
if ObjHasKey(image, "hwnd") {
image := image.hwnd
return "hwnd"
}
if ObjHasKey(image, "window") {
image := image.window
return "window"
}
if ObjHasKey(image, "base64") {
image := image.base64
return "base64"
}
if ObjHasKey(image, "sprite") {
image := image.sprite
return "sprite"
}
throw Exception("Invalid type.")
}
ImageType(ByRef image) {
; Must be first as ClipboardAll is just an empty string when passed through functions.
if this.is_clipboard(image)
return "clipboard"
; Throw if the image is an empty string.
if (image == "")
throw Exception("Image data is an empty string."
. "`nIf you passed ClipboardAll it does not contain compatible image data.")
if IsObject(image) {
; An "object" is an object that implements a Bitmap() method returning a pointer to a GDI+ bitmap.
if IsFunc(image.Bitmap)
return "object"
; A "buffer" is an AutoHotkey v2 buffer object.
if ObjHasKey(image, "pBitmap")
return "buffer"
; A "screenshot" is an array of 4 numbers.
if (image.1 ~= "^-?\d+$" && image.2 ~= "^-?\d+$" && image.3 ~= "^-?\d+$" && image.4 ~= "^-?\d+$")
return "screenshot"
}
; A "desktop" is a hidden window behind the desktop icons created by ImagePutDesktop.
if (image = "desktop")
return "desktop"
; A "wallpaper" is the desktop wallpaper.
if (image = "wallpaper")
return "wallpaper"
; A "cursor" is the name of a known cursor name.
if (image ~= "(?i)^(IDC|OCR)?_?(A_Cursor|AppStarting|Arrow|Cross|Help|IBeam|"
. "Icon|No|Size|SizeAll|SizeNESW|SizeNS|SizeNWSE|SizeWE|UpArrow|Wait|Unknown)$")
return "cursor"
; A "url" satisfies the url format.
if this.is_url(image)
return "url"
; A "file" must exist.
if FileExist(image)
return "file"
; A "bitmap" is a pointer to a GDI+ Bitmap.
if (DllCall("gdiplus\GdipGetImageType", "ptr", image, "ptr*", ErrorLevel) == 0)
return "bitmap"
; A "hBitmap" is a handle to a GDI Bitmap.
if (DllCall("GetObjectType", "ptr", image) == 7)
return "hBitmap"
; A "monitor" is a number like 0 and 1.
if (image = 0) ;if (image ~= "^\d+$" && image <= GetMonitorCount())
return "monitor"
; A "hwnd" is a handle to a window and more commonly known as ahk_id.
if DllCall("IsWindow", "ptr", image)
return "hwnd"
; A "window" is anything considered a Window Title including ahk_class and "A".
if WinExist(image)
return "window"
; A "base64" string is binary image data encoded into text using only 64 characters.
if (image ~= "^\s*(?:data:image\/[a-z]+;base64,)?"
. "(?:[A-Za-z0-9+\/]{4})*+(?:[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{2}==)?\s*$")
return "base64"
throw Exception("Image type could not be identified.")
}
toBitmap(type, ByRef image) {
if (type = "clipboard")
return this.from_clipboard()
if (type = "object")
return image.Bitmap()
if (type = "buffer") {
DllCall("gdiplus\GdipCloneImage", "ptr", image.pBitmap, "ptr*", pBitmap)
return pBitmap
}
if (type = "screenshot")
return this.from_screenshot(image)
if (type = "desktop")
return this.from_desktop()
if (type = "wallpaper")
return this.from_wallpaper()
if (type = "cursor")
return this.from_cursor()
if (type = "url")
return this.from_url(image)
if (type = "file") {
DllCall("gdiplus\GdipCreateBitmapFromFile", "wstr", image, "ptr*", pBitmap)
return pBitmap
}
if (type = "bitmap") {
DllCall("gdiplus\GdipCloneImage", "ptr", image, "ptr*", pBitmap)
return pBitmap
}
if (type = "hBitmap")
return this.from_hBitmap(image)
if (type = "monitor")
return this.from_monitor(image)
if (type = "hwnd" || type = "window") {
image := (type = "window") ? WinExist(image) : image
return this.from_hwnd(image)
}
if (type = "base64")
return this.from_base64(image)
if (type = "sprite")
return this.from_sprite(image)
throw Exception("Conversion from this type is not supported.")
}
toCotype(cotype, ByRef pBitmap, terms*) {
; toCotype("clipboard", pBitmap)
if (cotype = "clipboard")
return this.put_clipboard(pBitmap)
; toCotype("buffer", pBitmap)
if (cotype = "buffer") {
buffer := {__New: ObjBindMethod(this, "gdiplusStartup") ; Increment GDI+ reference count
, __Delete: ObjBindMethod(this, "gdiplusShutdown", "smart_pointer", pBitmap)}
buffer := new buffer ; On deletion the buffer object will dispose of the bitmap.
buffer.pBitmap := pBitmap ; And it will decrement this.gdiplus.
return buffer
}
; toCotype("screenshot", pBitmap, screenshot, alpha)
if (cotype = "screenshot")
return this.put_screenshot(pBitmap, terms.1, terms.2)
; toCotype("desktop", pBitmap)
if (cotype = "desktop")
return this.put_desktop(pBitmap)
; toCotype("wallpaper", pBitmap)
if (cotype = "wallpaper")
return this.put_wallpaper(pBitmap)
; toCotype("cursor", pBitmap, xHotspot, yHotspot)
if (cotype = "cursor")
return this.put_cursor(pBitmap, terms.1, terms.2)
; toCotype("url", ????????????????????????
if (cotype = "url") {
; put a url
}
; toCotype("file", pBitmap, filename, quality)
if (cotype = "file")
return this.put_file(pBitmap, terms.1, terms.2)
; toCotype("window", pBitmap)
if (cotype = "window")
return "ahk_id " . this.Render({"bitmap":pBitmap}).AlwaysOnTop().ToolWindow().Caption().hwnd
; toCotype("hwnd", pBitmap)
if (cotype = "hwnd")
return this.Render({"bitmap":pBitmap}).hwnd
; toCotype("bitmap", pBitmap)
if (cotype = "bitmap")
return pBitmap
; toCotype("hBitmap", pBitmap, alpha)
if (cotype = "hBitmap")
return this.put_hBitmap(pBitmap, terms.1)
; toCotype("base64", pBitmap, extension, quality)
if (cotype = "base64") ; Thanks to noname.
return this.put_base64(pBitmap, terms.1, terms.2)
throw Exception("Conversion to this type is not supported.")
}
DisposeImage(ByRef pBitmap) {
return DllCall("gdiplus\GdipDisposeImage", "ptr", pBitmap)
}
BitmapCrop(ByRef pBitmap, crop) {
; Get Bitmap width and height and format.
DllCall("gdiplus\GdipGetImageWidth", "ptr", pBitmap, "uint*", width)
DllCall("gdiplus\GdipGetImageHeight", "ptr", pBitmap, "uint*", height)
DllCall("gdiplus\GdipGetImagePixelFormat", "ptr", pBitmap, "ptr*", format)
; Are the numbers percentages?
crop.3 := (crop.3 ~= "%$") ? SubStr(crop.3, 1, -1) * 0.01 * width : crop.3
crop.4 := (crop.4 ~= "%$") ? SubStr(crop.4, 1, -1) * 0.01 * height : crop.4
crop.1 := (crop.1 ~= "%$") ? SubStr(crop.1, 1, -1) * 0.01 * width : crop.1
crop.2 := (crop.2 ~= "%$") ? SubStr(crop.2, 1, -1) * 0.01 * height : crop.2
; If numbers are negative, subtract the values from the edge.
crop.3 := (crop.3 < 0) ? width - Abs(crop.3) - Abs(crop.1) : crop.3
crop.4 := (crop.4 < 0) ? height - Abs(crop.4) - Abs(crop.2) : crop.4
crop.1 := Abs(crop.1)
crop.2 := Abs(crop.2)
; Round to the nearest integer.
crop.3 := Round(crop.1 + crop.3) - Round(crop.1) ; A reminder that width and height
crop.4 := Round(crop.2 + crop.4) - Round(crop.2) ; are distances, not coordinates.
crop.1 := Round(crop.1) ; so the abstract concept of a distance must be resolved
crop.2 := Round(crop.2) ; into coordinates and then rounded and added up again.
; Variance Shift. Now place x,y before w,h because we are building abstracts from reals now.
; Before we were resolving abstracts into real coordinates, now it's the opposite.
; Ensure that coordinates can never exceed the expected Bitmap area.
safe_x := (crop.1 > width) ? 0 : crop.1 ; Zero x if bigger.
safe_y := (crop.2 > height) ? 0 : crop.2 ; Zero y if bigger.
safe_w := (crop.1 + crop.3 > width) ? width - safe_x : crop.3 ; Max w if bigger.
safe_h := (crop.2 + crop.4 > height) ? height - safe_y : crop.4 ; Max h if bigger.
; Clone
DllCall("gdiplus\GdipCloneBitmapAreaI"
, "int", safe_x
, "int", safe_y
, "int", safe_w
, "int", safe_h
, "int", format
, "ptr", pBitmap
, "ptr*", pBitmapCrop)
return pBitmapCrop
}
BitmapScale(ByRef pBitmap, scale) {
; Get Bitmap width and height and format.
DllCall("gdiplus\GdipGetImageWidth", "ptr", pBitmap, "uint*", width)
DllCall("gdiplus\GdipGetImageHeight", "ptr", pBitmap, "uint*", height)
DllCall("gdiplus\GdipGetImagePixelFormat", "ptr", pBitmap, "ptr*", format)
safe_w := Ceil(width * scale)
safe_h := Ceil(height * scale)
; Create a new bitmap and get the graphics context.
DllCall("gdiplus\GdipCreateBitmapFromScan0"
, "int", safe_w, "int", safe_h, "int", 0, "int", format, "ptr", 0, "ptr*", pBitmapScale)
DllCall("gdiplus\GdipGetImageGraphicsContext", "ptr", pBitmapScale, "ptr*", pGraphics)
; Set settings in graphics context.
DllCall("gdiplus\GdipSetPixelOffsetMode", "ptr", pGraphics, "int", 2) ; Half pixel offset.
DllCall("gdiplus\GdipSetCompositingMode", "ptr", pGraphics, "int", 1) ; Overwrite/SourceCopy.
DllCall("gdiplus\GdipSetInterpolationMode", "ptr", pGraphics, "int", 7) ; HighQualityBicubic
; Draw Image. Not sure why the integer variant fails below.
DllCall("gdiplus\GdipCreateImageAttributes", "ptr*", ImageAttr)
DllCall("gdiplus\GdipSetImageAttributesWrapMode", "ptr", ImageAttr, "int", 3) ; WrapModeTileFlipXY
DllCall("gdiplus\GdipDrawImageRectRectI"
, "ptr", pGraphics
, "ptr", pBitmap
, "int", 0, "int", 0, "int", safe_w, "int", safe_h ; destination rectangle
, "int", 0, "int", 0, "int", width, "int", height ; source rectangle
, "int", 2
, "ptr", ImageAttr
, "ptr", 0
, "ptr", 0)
DllCall("gdiplus\GdipDisposeImageAttributes", "ptr", ImageAttr)
; Clean up the graphics context.
DllCall("gdiplus\GdipDeleteGraphics", "ptr", pGraphics)
return pBitmapScale
}
is_clipboard(c) {
; ClipboardAll is always an empty string when passed into a function.
if (c != "") ; Must be an empty string.
return false
; Look through the clipboard for a memory object containing a BITMAPINFO structure followed by the bitmap bits.
if DllCall("OpenClipboard", "ptr", 0) {
_answer := DllCall("IsClipboardFormatAvailable", "uint", 8) ; CF_DIB
DllCall("CloseClipboard")
if (_answer)
return true
}
; Error messages are inaccurate and it can't be helped.
return false
}
is_url(url) {
; Thanks splattermania - https://www.php.net/manual/en/function.preg-match.php#93824
regex := "^(?i)"
. "((https?|ftp)\:\/\/)" ; SCHEME
. "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?" ; User and Pass
. "([a-z0-9-.]*)\.([a-z]{2,3})" ; Host or IP
. "(\:[0-9]{2,5})?" ; Port
. "(\/(?:[a-z0-9-_~!$&'()*+,;=:@]\.?)+)*\/?" ; Path
. "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?" ; GET Query
. "(#[a-z_.-][a-z0-9+\$_.-]*)?$" ; Anchor
return (url ~= regex)
}
from_clipboard() {
; Thanks tic - https://www.autohotkey.com/boards/viewtopic.php?t=6517
if DllCall("OpenClipboard", "ptr", 0) {
hBitmap := DllCall("GetClipboardData", "uint", 2, "ptr")
DllCall("CloseClipboard")
DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "ptr", hBitmap, "ptr", 0, "ptr*", pBitmap)
DllCall("DeleteObject", "ptr", hBitmap)
}
return pBitmap
}
from_screenshot(ByRef image) {
; Thanks tic - https://www.autohotkey.com/boards/viewtopic.php?t=6517
; struct BITMAPINFOHEADER - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
VarSetCapacity(bi, 40, 0) ; sizeof(bi) = 40
, NumPut( 40, bi, 0, "uint") ; Size
, NumPut( image.3, bi, 4, "uint") ; Width
, NumPut(-image.4, bi, 8, "int") ; Height - Negative so (0, 0) is top-left.
, NumPut( 1, bi, 12, "ushort") ; Planes
, NumPut( 32, bi, 14, "ushort") ; BitCount / BitsPerPixel
hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", &bi, "uint", 0, "ptr*", pBits, "ptr", 0, "uint", 0, "ptr")
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
; Retrieve the device context for the screen.
sdc := DllCall("GetDC", "ptr", 0, "ptr")
; Copies a portion of the screen to a new device context.
DllCall("gdi32\BitBlt"
, "ptr", hdc, "int", 0, "int", 0, "int", image.3, "int", image.4
, "ptr", sdc, "int", image.1, "int", image.2, "uint", 0x00CC0020) ; SRCCOPY
; Release the device context to the screen.
DllCall("ReleaseDC", "ptr", 0, "ptr", sdc)
; Convert the hBitmap to a Bitmap using a built in function as there is no transparency.
DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "ptr", hbm, "ptr", 0, "ptr*", pBitmap)
; Cleanup the hBitmap and device contexts.
DllCall("SelectObject", "ptr", hdc, "ptr", obm)
DllCall("DeleteObject", "ptr", hbm)
DllCall("DeleteDC", "ptr", hdc)
return pBitmap
}
from_wallpaper() {
; Get the width and height of all monitors.
width := DllCall("GetSystemMetrics", "int", 78)
height := DllCall("GetSystemMetrics", "int", 79)
; struct BITMAPINFOHEADER - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
VarSetCapacity(bi, 40, 0) ; sizeof(bi) = 40
, NumPut( 40, bi, 0, "uint") ; Size
, NumPut( width, bi, 4, "uint") ; Width
, NumPut( -height, bi, 8, "int") ; Height - Negative so (0, 0) is top-left.
, NumPut( 1, bi, 12, "ushort") ; Planes
, NumPut( 32, bi, 14, "ushort") ; BitCount / BitsPerPixel
hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", &bi, "uint", 0, "ptr*", pBits, "ptr", 0, "uint", 0, "ptr")
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
; Paints the desktop.
DllCall("PaintDesktop", "ptr", hdc)
; Convert the hBitmap to a Bitmap using a built in function as there is no transparency.
DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "ptr", hbm, "ptr", 0, "ptr*", pBitmap)
; Cleanup the hBitmap and device contexts.
DllCall("SelectObject", "ptr", hdc, "ptr", obm)
DllCall("DeleteObject", "ptr", hbm)
DllCall("DeleteDC", "ptr", hdc)
return pBitmap
}
from_cursor() {
; Thanks 23W - https://stackoverflow.com/a/13295280
; struct CURSORINFO - https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-cursorinfo
NumPut(VarSetCapacity(ci, 16+A_PtrSize, 0), ci, "int") ; sizeof(CURSORINFO) = 20, 24
DllCall("GetCursorInfo", "ptr", &ci)
; cShow := NumGet(ci, 4, "int") ; 0x1 = CURSOR_SHOWING, 0x2 = CURSOR_SUPPRESSED
, hCursor := NumGet(ci, 8, "ptr")
; xCursor := NumGet(ci, 8+A_PtrSize, "int")
; yCursor := NumGet(ci, 12+A_PtrSize, "int")
; struct ICONINFO - https://docs.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-iconinfo
VarSetCapacity(ii, 8+3*A_PtrSize, 0) ; sizeof(ICONINFO) = 20, 32
DllCall("GetIconInfo", "ptr", hCursor, "ptr", &ii)
; xHotspot := NumGet(ii, 4, "uint")
; yHotspot := NumGet(ii, 8, "uint")
, hbmMask := NumGet(ii, 8+A_PtrSize, "ptr") ; x86:12, x64:16
, hbmColor := NumGet(ii, 8+2*A_PtrSize, "ptr") ; x86:16, x64:24
; struct BITMAP - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmap
DllCall("GetObject"
, "ptr", hbmMask
, "int", VarSetCapacity(bm, 16+2*A_PtrSize) ; sizeof(BITMAP) = 24, 32
, "ptr", &bm)
, width := NumGet(bm, 4, "uint")
, height := NumGet(bm, 8, "uint") / (hbmColor ? 1 : 2) ; Black and White cursors have doubled height.
; Clean up these hBitmaps.
DllCall("DeleteObject", "ptr", hbmMask)
DllCall("DeleteObject", "ptr", hbmColor)
; struct BITMAPINFOHEADER - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
VarSetCapacity(bi, 40, 0) ; sizeof(bi) = 40
, NumPut( 40, bi, 0, "uint") ; Size
, NumPut( width, bi, 4, "uint") ; Width
, NumPut( -height, bi, 8, "int") ; Height - Negative so (0, 0) is top-left.
, NumPut( 1, bi, 12, "ushort") ; Planes
, NumPut( 32, bi, 14, "ushort") ; BitCount / BitsPerPixel
hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", &bi, "uint", 0, "ptr*", pBits, "ptr", 0, "uint", 0, "ptr")
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
; This is the 32-bit ARGB pBitmap (different from an hBitmap) that will receive the final converted pixels.
DllCall("gdiplus\GdipCreateBitmapFromScan0"
, "int", width, "int", height, "int", 0, "int", 0x26200A, "ptr", 0, "ptr*", pBitmap)
; Create a Scan0 buffer pointing to pBits. The buffer has pixel format pARGB.
VarSetCapacity(Rect, 16, 0) ; sizeof(Rect) = 16
, NumPut( width, Rect, 8, "uint") ; Width
, NumPut( height, Rect, 12, "uint") ; Height
VarSetCapacity(BitmapData, 16+2*A_PtrSize, 0) ; sizeof(BitmapData) = 24, 32
, NumPut( width, BitmapData, 0, "uint") ; Width
, NumPut( height, BitmapData, 4, "uint") ; Height
, NumPut( 4 * width, BitmapData, 8, "int") ; Stride
, NumPut( 0xE200B, BitmapData, 12, "int") ; PixelFormat
, NumPut( pBits, BitmapData, 16, "ptr") ; Scan0
; Use LockBits to create a writable buffer that converts pARGB to ARGB.
DllCall("gdiplus\GdipBitmapLockBits"
, "ptr", pBitmap
, "ptr", &Rect
, "uint", 6 ; ImageLockMode.UserInputBuffer | ImageLockMode.WriteOnly
, "int", 0xE200B ; Format32bppPArgb
, "ptr", &BitmapData) ; Contains the pointer (pBits) to the hbm.
; Don't use DI_DEFAULTSIZE to draw the icon like DrawIcon does as it will resize to 32 x 32.
DllCall("DrawIconEx"
, "ptr", hdc, "int", 0, "int", 0
, "ptr", hCursor, "int", 0, "int", 0
, "uint", 0, "ptr", 0, "uint", 0x1 | 0x2 | 0x4) ; DI_MASK | DI_IMAGE | DI_COMPAT
; Convert the pARGB pixels copied into the device independent bitmap (hbm) to ARGB.
DllCall("gdiplus\GdipBitmapUnlockBits", "ptr", pBitmap, "ptr", &BitmapData)
; Clean up the icon and device context.
DllCall("DestroyIcon", "ptr", hCursor)
DllCall("SelectObject", "ptr", hdc, "ptr", obm)
DllCall("DeleteObject", "ptr", hbm)
DllCall("DeleteDC", "ptr", hdc)
return pBitmap
}
from_url(ByRef image) {
req := ComObjCreate("WinHttp.WinHttpRequest.5.1")
req.Open("GET", image)
req.Send()
pStream := ComObjQuery(req.ResponseStream, "{0000000C-0000-0000-C000-000000000046}")
DllCall("gdiplus\GdipCreateBitmapFromStream", "ptr", pStream, "ptr*", pBitmap)
ObjRelease(pStream)
return pBitmap
}
from_hBitmap(ByRef image) {
; struct BITMAP - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmap
DllCall("GetObject"
, "ptr", image
, "int", VarSetCapacity(dib, 76+2*(A_PtrSize=8?4:0)+2*A_PtrSize)
, "ptr", &dib) ; sizeof(DIBSECTION) = 84, 104
, width := NumGet(dib, 4, "uint")
, height := NumGet(dib, 8, "uint")
, bpp := NumGet(dib, 18, "ushort")
; Fallback to built-in method if pixels are not 32-bit ARGB.
if (bpp != 32) { ; This built-in version is 120% faster but ignores transparency.
DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "ptr", image, "ptr", 0, "ptr*", pBitmap)
return pBitmap
}
; Create a handle to a device context and associate the image.
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr") ; Creates a memory DC compatible with the current screen.
obm := DllCall("SelectObject", "ptr", hdc, "ptr", image, "ptr") ; Put the (hBitmap) image onto the device context.
; Create a device independent bitmap with negative height. All DIBs use the screen pixel format (pARGB).
; Use hbm to buffer the image such that top-down and bottom-up images are mapped to this top-down buffer.
cdc := DllCall("CreateCompatibleDC", "ptr", hdc, "ptr")
VarSetCapacity(bi, 40, 0) ; sizeof(bi) = 40
, NumPut( 40, bi, 0, "uint") ; Size
, NumPut( width, bi, 4, "uint") ; Width
, NumPut( -height, bi, 8, "int") ; Height - Negative so (0, 0) is top-left.
, NumPut( 1, bi, 12, "ushort") ; Planes
, NumPut( 32, bi, 14, "ushort") ; BitCount / BitsPerPixel
hbm := DllCall("CreateDIBSection", "ptr", cdc, "ptr", &bi, "uint", 0
, "ptr*", pBits ; pBits is the pointer to (top-down) pixel values.
, "ptr", 0, "uint", 0, "ptr")
ob2 := DllCall("SelectObject", "ptr", cdc, "ptr", hbm, "ptr")
; This is the 32-bit ARGB pBitmap (different from an hBitmap) that will receive the final converted pixels.
DllCall("gdiplus\GdipCreateBitmapFromScan0"
, "int", width, "int", height, "int", 0, "int", 0x26200A, "ptr", 0, "ptr*", pBitmap)
; Create a Scan0 buffer pointing to pBits. The buffer has pixel format pARGB.
VarSetCapacity(Rect, 16, 0) ; sizeof(Rect) = 16
, NumPut( width, Rect, 8, "uint") ; Width
, NumPut( height, Rect, 12, "uint") ; Height
VarSetCapacity(BitmapData, 16+2*A_PtrSize, 0) ; sizeof(BitmapData) = 24, 32
, NumPut( width, BitmapData, 0, "uint") ; Width
, NumPut( height, BitmapData, 4, "uint") ; Height
, NumPut( 4 * width, BitmapData, 8, "int") ; Stride
, NumPut( 0xE200B, BitmapData, 12, "int") ; PixelFormat
, NumPut( pBits, BitmapData, 16, "ptr") ; Scan0
; Use LockBits to create a writable buffer that converts pARGB to ARGB.
DllCall("gdiplus\GdipBitmapLockBits"
, "ptr", pBitmap
, "ptr", &Rect
, "uint", 6 ; ImageLockMode.UserInputBuffer | ImageLockMode.WriteOnly
, "int", 0xE200B ; Format32bppPArgb
, "ptr", &BitmapData) ; Contains the pointer (pBits) to the hbm.
; Copies the image (hBitmap) to a top-down bitmap. Removes bottom-up-ness if present.
DllCall("gdi32\BitBlt"
, "ptr", cdc, "int", 0, "int", 0, "int", width, "int", height
, "ptr", hdc, "int", 0, "int", 0, "uint", 0x00CC0020) ; SRCCOPY
; Convert the pARGB pixels copied into the device independent bitmap (hbm) to ARGB.
DllCall("gdiplus\GdipBitmapUnlockBits", "ptr", pBitmap, "ptr", &BitmapData)
; Cleanup the buffer and device contexts.
DllCall("SelectObject", "ptr", cdc, "ptr", ob2)
DllCall("DeleteObject", "ptr", hbm)
DllCall("DeleteDC", "ptr", cdc)
DllCall("SelectObject", "ptr", hdc, "ptr", obm)
DllCall("DeleteDC", "ptr", hdc)
return pBitmap
}
from_monitor(ByRef image) {
if (image > 0) {
; M := GetMonitorInfo(image)
x := M.Left
y := M.Top
w := M.Right - M.Left
h := M.Bottom - M.Top
} else {
x := DllCall("GetSystemMetrics", "int", 76)
y := DllCall("GetSystemMetrics", "int", 77)
w := DllCall("GetSystemMetrics", "int", 78)
h := DllCall("GetSystemMetrics", "int", 79)
}
return this.from_screenshot([x,y,w,h])
}
from_hwnd(ByRef image) {
; Thanks tic - https://www.autohotkey.com/boards/viewtopic.php?t=6517
; Restore the window if minimized! Must be visible for capture.
if DllCall("IsIconic", "ptr", image)
DllCall("ShowWindow", "ptr", image, "int", 4)
; Get the width and height of the client window.
VarSetCapacity(rect, 16) ; sizeof(RECT) = 16
DllCall("GetClientRect", "ptr", image, "ptr", &rect)
, width := NumGet(rect, 8, "int")
, height := NumGet(rect, 12, "int")
; struct BITMAPINFOHEADER - https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
VarSetCapacity(bi, 40, 0) ; sizeof(bi) = 40
, NumPut( 40, bi, 0, "uint") ; Size
, NumPut( width, bi, 4, "uint") ; Width
, NumPut( -height, bi, 8, "int") ; Height - Negative so (0, 0) is top-left.
, NumPut( 1, bi, 12, "ushort") ; Planes
, NumPut( 32, bi, 14, "ushort") ; BitCount / BitsPerPixel
hbm := DllCall("CreateDIBSection", "ptr", hdc, "ptr", &bi, "uint", 0, "ptr*", pBits, "ptr", 0, "uint", 0, "ptr")
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
; Print the window onto the hBitmap using an undocumented flag. https://stackoverflow.com/a/40042587
DllCall("PrintWindow", "ptr", image, "ptr", hdc, "uint", 0x3) ; PW_CLIENTONLY | PW_RENDERFULLCONTENT
; Additional info on how this is implemented: https://www.reddit.com/r/windows/comments/8ffr56/altprintscreen/
; Convert the hBitmap to a Bitmap using a built in function as there is no transparency.
DllCall("gdiplus\GdipCreateBitmapFromHBITMAP", "ptr", hbm, "ptr", 0, "ptr*", pBitmap)
; Cleanup the hBitmap and device contexts.
DllCall("SelectObject", "ptr", hdc, "ptr", obm)
DllCall("DeleteObject", "ptr", hbm)
DllCall("DeleteDC", "ptr", hdc)
return pBitmap
}
from_base64(ByRef image) {
; Trim whitespace and remove header.
image := Trim(image)
image := RegExReplace(image, "^data:image\/[a-z]+;base64,")
; Converts the image to binary data by first asking for the size.
DllCall("crypt32\CryptStringToBinary", "ptr",&image, "uint",0, "uint",0x1, "ptr", 0, "uint*",size, "ptr",0, "ptr",0)
VarSetCapacity(bin, size, 0)
DllCall("crypt32\CryptStringToBinary", "ptr",&image, "uint",0, "uint",0x1, "ptr",&bin, "uint*",size, "ptr",0, "ptr",0)
; Makes a stream for conversion into a pBitmap.
pStream := DllCall("shlwapi\SHCreateMemStream", "ptr", &bin, "uint", size, "ptr")
DllCall("gdiplus\GdipCreateBitmapFromStream", "ptr", pStream, "ptr*", pBitmap)
ObjRelease(pStream)
return pBitmap
}
from_sprite(ByRef image) {
; Create a source pBitmap and extract the width and height.
if DllCall("gdiplus\GdipCreateBitmapFromFile", "wstr", image, "ptr*", sBitmap)
if !(sBitmap := this.from_url(image))
throw Exception("Could not be loaded from a valid file path or URL.")
; Get Bitmap width and height.
DllCall("gdiplus\GdipGetImageWidth", "ptr", sBitmap, "uint*", width)
DllCall("gdiplus\GdipGetImageHeight", "ptr", sBitmap, "uint*", height)
; Create a destination pBitmap in 32-bit ARGB and get its device context though GDI+.
; Note that a device context from a graphics context can only be drawn on, not read.
; Also note that using a graphics context and blitting does not create a pixel perfect image.
; Using a DIB and LockBits is about 5% faster.
DllCall("gdiplus\GdipCreateBitmapFromScan0"
, "int", width, "int", height, "int", 0, "int", 0x26200A, "ptr", 0, "ptr*", dBitmap)
DllCall("gdiplus\GdipGetImageGraphicsContext", "ptr", dBitmap, "ptr*", dGraphics)
DllCall("gdiplus\GdipGetDC", "ptr", dGraphics, "ptr*", ddc)
; Keep any existing transparency for whatever reason.
hBitmap := this.put_hBitmap(sBitmap) ; Could copy this code here for even more speed.
; Create a source device context and associate the source hBitmap.
sdc := DllCall("CreateCompatibleDC", "ptr", ddc, "ptr")
obm := DllCall("SelectObject", "ptr", sdc, "ptr", hBitmap, "ptr")
; Copy the image making the top-left pixel the color key.
DllCall("msimg32\TransparentBlt"
, "ptr", ddc, "int", 0, "int", 0, "int", width, "int", height ; destination
, "ptr", sdc, "int", 0, "int", 0, "int", width, "int", height ; source
, "uint", DllCall("GetPixel", "ptr", sdc, "int", 0, "int", 0)) ; RGB pixel.
; Cleanup the hBitmap and device contexts.
DllCall("SelectObject", "ptr", sdc, "ptr", obm)
DllCall("DeleteObject", "ptr", hBitmap)
DllCall("DeleteDC", "ptr", sdc)
; Release the graphics context and delete.
DllCall("gdiplus\GdipReleaseDC", "ptr", dGraphics, "ptr", ddc)
DllCall("gdiplus\GdipDeleteGraphics", "ptr", dGraphics)
return dBitmap
}
put_clipboard(ByRef pBitmap) {
; Thanks tic - https://www.autohotkey.com/boards/viewtopic.php?t=6517
off1 := A_PtrSize = 8 ? 52 : 44, off2 := A_PtrSize = 8 ? 32 : 24
DllCall("gdiplus\GdipCreateHBITMAPFromBitmap", "ptr", pBitmap, "ptr*", hBitmap, "uint", 0xFFFFFFFF)
DllCall("GetObject", "ptr", hBitmap, "int", VarSetCapacity(oi, A_PtrSize = 8 ? 104 : 84, 0), "ptr", &oi)
hdib := DllCall("GlobalAlloc", "uint", 2, "ptr", 40+NumGet(oi, off1, "uint"), "ptr")
pdib := DllCall("GlobalLock", "ptr", hdib, "ptr")
DllCall("RtlMoveMemory", "ptr", pdib, "ptr", &oi+off2, "uptr", 40)
DllCall("RtlMoveMemory", "ptr", pdib+40, "ptr", NumGet(oi, off2 - (A_PtrSize ? A_PtrSize : 4), "ptr"), "uptr", NumGet(oi, off1, "uint"))
DllCall("GlobalUnlock", "ptr", hdib)
DllCall("DeleteObject", "ptr", hBitmap)
DllCall("OpenClipboard", "ptr", 0)
DllCall("EmptyClipboard")
DllCall("SetClipboardData", "uint", 8, "ptr", hdib)
DllCall("CloseClipboard")
; Returns an empty string as ClipboardAll would also be an empty string.
return ""
}
put_screenshot(ByRef pBitmap, screenshot := "", alpha := "") {
; Get Bitmap width and height.
DllCall("gdiplus\GdipGetImageWidth", "ptr", pBitmap, "uint*", width)
DllCall("gdiplus\GdipGetImageHeight", "ptr", pBitmap, "uint*", height)
x := (screenshot.1 != "") ? screenshot.1 : Round((A_ScreenWidth - width) / 2)
y := (screenshot.2 != "") ? screenshot.2 : Round((A_ScreenHeight - height) / 2)
w := (screenshot.3 != "") ? screenshot.3 : width
h := (screenshot.4 != "") ? screenshot.4 : height
; Convert the Bitmap to a hBitmap and associate a device context for blitting.
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
hbm := this.put_hBitmap(pBitmap, alpha)
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
; Get device context of spawned window.
ddc := DllCall("GetDC", "ptr", 0, "ptr")
; Copies a portion of the screen to a new device context.
DllCall("gdi32\StretchBlt"
, "ptr", ddc, "int", x, "int", y, "int", w, "int", h
, "ptr", hdc, "int", 0, "int", 0, "int", width, "int", height
, "uint", 0x00CC0020) ; SRCCOPY
; Release device context of spawned window.
DllCall("ReleaseDC", "ptr", 0, "ptr", ddc)
; Cleanup the hBitmap and device contexts.
DllCall("SelectObject", "ptr", hdc, "ptr", obm)
DllCall("DeleteObject", "ptr", hbm)
DllCall("DeleteDC", "ptr", hdc)
return [x,y,w,h]
}
put_desktop(ByRef pBitmap) {
; Thanks Gerald Degeneve - https://www.codeproject.com/Articles/856020/Draw-Behind-Desktop-Icons-in-Windows-plus
; Get Bitmap width and height.
DllCall("gdiplus\GdipGetImageWidth", "ptr", pBitmap, "uint*", width)
DllCall("gdiplus\GdipGetImageHeight", "ptr", pBitmap, "uint*", height)
; Convert the Bitmap to a hBitmap and associate a device context for blitting.
hdc := DllCall("CreateCompatibleDC", "ptr", 0, "ptr")
hbm := this.put_hBitmap(pBitmap)
obm := DllCall("SelectObject", "ptr", hdc, "ptr", hbm, "ptr")
; Post-Creator's Update Windows 10. WM_SPAWN_WORKER = 0x052C
DllCall("SendMessage", "ptr", WinExist("ahk_class Progman"), "uint", 0x052C, "ptr", 0x0000000D, "ptr", 0)
DllCall("SendMessage", "ptr", WinExist("ahk_class Progman"), "uint", 0x052C, "ptr", 0x0000000D, "ptr", 1)
; Find the child window.
WinGet windows, List, ahk_class WorkerW
Loop % windows
hwnd := windows%A_Index%
until DllCall("FindWindowEx", "ptr", hwnd, "ptr", 0, "str", "SHELLDLL_DefView", "ptr", 0)
WorkerW := DllCall("FindWindowEx", "ptr", 0, "ptr", hwnd, "str", "WorkerW", "ptr", 0, "ptr")