-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuiEnhancerKit.ahk
873 lines (737 loc) · 36.3 KB
/
GuiEnhancerKit.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
/************************************************************************
* @description Elevate your AHK Gui development with extended methods and properties.
* @file GuiEnhancerKit.ahk
* @author Nikola Perovic
* @link https://github.com/nperovic/GuiEnhancerKit
* @date 2024/06/16
* @version 1.0.0
***********************************************************************/
#Requires AutoHotkey v2
#DllLoad gdi32.dll
#DllLoad uxtheme.dll
#DllLoad dwmapi.dll
class GuiExt extends Gui
{
class __Struct extends Buffer
{
__New(ByteCount?, FillByte?) => super.__New(ByteCount?, FillByte?)
Set(ptr?)
{
if !(ptr??0)
return
for p, v in ptr.OwnProps()
if this.HasProp(p)
this.%p% := v
}
PropDesc(name, ofst, type, ptr?)
{
if ((ptr??0) && IsNumber(ptr))
NumPut(type, NumGet(ptr, ofst, type), this, ofst)
this.DefineProp(name, {
Get: NumGet.Bind(, ofst, type),
Set: (p, v) => NumPut(type, v, this, ofst)
})
}
}
class RECT extends GuiExt.__Struct
{
/**
* The `RECT` structure defines a rectangle by the coordinates of its upper-left and lower-right corners.
* @param {object|integer} [objOrAddress] *Optional:* Create rect object and set values to each property. It can be object or the `ptr` address.
* @example
* DllCall("GetWindowRect", "Ptr", WinExist("A"), "ptr", rc := GuiExt.RECT())
* MsgBox rc.left " " rc.top " " rc.right " " rc.bottom
*
* @example
* rc := GuiExt.RECT({top: 10, bottom: 69})
* MsgBox "L" rc.left "/ T" rc.top "/ R" rc.right "/ B" rc.bottom ; L0/ T10/ R0/ B69
*
* @example
* myGui.OnMessage(WM_NCCALCSIZE := 0x0083, NCCALCSIZE)
* NCCALCSIZE(guiObj, wParam, lParam, msg)
* {
* if !wParam {
* rc := GuiExt.RECT(lParam)
* ToolTip "L" rc.left "/ T" rc.top "/ R" rc.right "/ B" rc.bottom
* }
* }
*
* @returns The Buffer object that defined the `RECT` structure.
* @link [Learn more on MSDN](https://learn.microsoft.com/en-us/windows/win32/api/windef/ns-windef-rect)
*/
__New(objOrAddress?)
{
super.__New(16)
(IsSet(objOrAddress) && IsNumber(objOrAddress) && (ptr := objOrAddress))
(IsSet(objOrAddress) && IsObject(objOrAddress) && (objOrAddress := objOrAddress))
for i, prop in ["left", "top", "right", "bottom"]
this.PropDesc(prop, 4 * (i-1), "int", ptr?)
this.Set(objOrAddress?)
}
/** @prop {integer} left Specifies the x-coordinate of the upper-left corner of the rectangle. */
left := unset
/** @prop {integer} top Specifies the y-coordinate of the upper-left corner of the rectangle. */
top := unset
/** @prop {integer} right Specifies the x-coordinate of the lower-right corner of the rectangle. */
right := unset
/** @prop {integer} bottom Specifies the y-coordinate of the lower-right corner of the rectangle. */
bottom := unset
/** @prop {integer} width Rect width. */
width => (this.right - this.left)
/** @prop {integer} width Rect width. */
height => (this.bottom - this.top)
}
static __New()
{
GuiExt.Control.__New(p := this.Prototype, sp := super.Prototype)
for _p in [sp, this.Control.Prototype, Gui.Control.Prototype]
for prop in ["x", "y", "w", "h"]
_p.DefineProp(prop, {Get: p.__GetPos.Bind(, prop), Set: p.__SetPos.Bind(, prop)})
}
/**
* Create a new Gui object.
* @param Options AlwaysOnTop Border Caption Disabled -DPIScale LastFound
* MaximizeBox MinimizeBox MinSize600x600 MaxSize800x800 Resize
* OwnDialogs '+Owner' OtherGui.hwnd +Parent
* SysMenu Theme ToolWindow
* @param Title The window title. If omitted, it defaults to the current value of A_ScriptName.
* @param EventObj OnEvent, OnNotify and OnCommand can be used to register methods of EventObj to be called when an event is raised
* @returns {GuiExt|Gui}
*/
__New(Options := '', Title := A_ScriptName, EventObj?) => super.__New(Options?, Title?, EventObj??this)
/**
* @prop {Integer} X X position
* @prop {Integer} Y Y position
* @prop {Integer} W Width
* @prop {Integer} H Height
*/
X := Y := W := H := 0
/**
* Create controls such as text, buttons or checkboxes, and return a GuiControl object.
* @param {'ActiveX'|'Button'|'Checkbox'|'ComboBox'|'Custom'|'DateTime'|'DropDownList'|'Edit'|'GroupBox'|'Hotkey'|'Link'|'ListBox'|'ListView'|'MonthCal'|'Picture'|'Progress'|'Radio'|'Slider'|'StatusBar'|'Tab'|'Tab2'|'Tab3'|'Text'|'TreeView'|'UpDown'} ControlType
* @param Options V: Sets the control's Name.
* Pos: xn yn wn hn rn Right Left Center Section
* VScroll HScroll -Tabstop -Wrap
* BackgroundColor Border Theme Disabled Hidden
* @returns {GuiExt.Control|GuiExt.ActiveX|GuiExt.Button|GuiExt.Checkbox|GuiExt.ComboBox|GuiExt.Custom|GuiExt.DateTime|GuiExt.DropDownList|GuiExt.Edit|GuiExt.GroupBox|GuiExt.Hotkey|GuiExt.Link|GuiExt.ListBox|GuiExt.ListView|GuiExt.MonthCal|GuiExt.Picture|GuiExt.Progress|GuiExt.Radio|GuiExt.Slider|GuiExt.StatusBar|GuiExt.Tab|GuiExt.Tab2|GuiExt.Tab3|GuiExt.Text|GuiExt.TreeView|GuiExt.UpDown}
*/
Add(ControlType, Options?, Text?) => super.Add(ControlType, Options?, Text?)
__GetPos(prop) => (this.GetPos(&x, &y, &w, &h), %prop%)
__SetPos(prop, value)
{
SetWinDelay(-1), SetControlDelay(-1)
try %prop% := value
try this.Move(x?, y?, w?, h?)
}
/**
* To create a borderless window with customizable resizing behavior.
* @param {Integer} [border=6] The width of the edge of the window where the window size can be adjusted. If this value is `0`, the window will not be resizable.
* @param {(guiObj, x, y) => Integer} [DragWndFunc=""] A callback function used to check whether the window is currently in a drag state. If the function returns `true` and the left mouse button is held down on the `Gui` window, the effect is the same as holding down the left button on the window title bar.
* @param {number} [cxLeftWidth] The width of the left border that retains its size.
* @param {number} [cxRightWidth] The width of the right border that retains its size.
* @param {number} [cyTopHeight] The height of the top border that retains its size.
* @param {number} [cyBottomHeight] The height of the bottom border that retains its size.
*/
SetBorderless(border := 6, dragWndFunc := "", cxLeftWidth?, cxRightWidth?, cyTopHeight?, cyBottomHeight?)
{
static WM_NCCALCSIZE := 0x83
static WM_NCHITTEST := 0x84
static WM_NCACTIVATE := 0x86
static WM_ACTIVATE := 0x6
this.SetWindowAttribute(3, 1)
; Set Rounded Corner for Windows 11
if (VerCompare(A_OSVersion, "10.0.22000") >= 0)
this.SetWindowAttribute(33, 2)
this.OnMessage(WM_ACTIVATE, CB_ACTIVATE)
this.OnMessage(WM_NCACTIVATE, CB_NCACTIVATE)
this.OnMessage(WM_NCCALCSIZE, CB_NCCALCSIZE)
; Make window resizable.
this.OnMessage(WM_NCHITTEST, CB_NCHITTEST.Bind(dragWndFunc ? dragWndFunc.Bind(this) : 0))
ExtendFrameIntoClientArea(cxLeftWidth?, cxRightWidth?, cyTopHeight?, cyBottomHeight?)
CB_ACTIVATE(g, wParam, lParam, Msg)
{
SetWinDelay(-1), SetControlDelay(-1), WinRedraw(g)
if (lParam = g.hwnd && (wParam & 0xFFFF))
ExtendFrameIntoClientArea(cxLeftWidth?, cxRightWidth?, cyTopHeight?, cyBottomHeight?)
}
CB_NCCALCSIZE(g, wParam, lParam, Msg)
{
if wParam
return 0
}
CB_NCACTIVATE(g, wParam, lParam, *)
{
if !wParam
return true
if (lParam != g.hwnd) && GetKeyState("LButton", "P")
return false
SetWinDelay(-1)
WinRedraw(g)
}
/**
* @param {Function} HTFunc
* @param {GuiExt} g
* @param {integer} wParam
* @param {integer} lParam
* @param {integer} Msg
* @returns {Integer | unset}
*/
CB_NCHITTEST(HTFunc?, g?, wParam?, lParam?, Msg?)
{
static HTLEFT := 10, HTRIGHT := 11
, HTTOP := 12, HTTOPLEFT := 13
, HTTOPRIGHT := 14, HTBOTTOM := 15
, HTBOTTOMLEFT := 16, HTBOTTOMRIGHT := 17
, TCAPTION := 2
if !(g is Gui)
return
CoordMode("Mouse")
MouseGetPos(&x, &y)
rc := g.GetWindowRect()
R := (x >= rc.right - border)
L := (x < rc.left + border)
if (B := (y >= rc.bottom - border))
return R ? HTBOTTOMRIGHT: L ? HTBOTTOMLEFT: HTBOTTOM
if (T := (y < rc.top + border))
return R ? HTTOPRIGHT: L ? HTTOPLEFT: HTTOP
return L ? HTLEFT: R ? HTRIGHT: (HTFunc && HTFunc(x, y) ? TCAPTION : (_ := unset))
}
ExtendFrameIntoClientArea(cxLeftWidth?, cxRightWidth?, cyTopHeight?, cyBottomHeight?)
{
rc := this.GetWindowRect()
NumPut('int', cxLeftWidth ?? rc.width,'int', cxRightWidth ?? rc.width,'int', cyTopHeight ?? rc.height,'int', cyBottomHeight ?? rc.height, margin := Buffer(16))
DllCall("Dwmapi\DwmExtendFrameIntoClientArea", "Ptr", this.hWnd, "Ptr", margin)
}
}
/**
* Retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
* [Learn more](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindowrect)
* @returns {RECT}
*/
GetWindowRect() => (DllCall("GetWindowRect", "ptr", this.hwnd, "ptr", _rc := GuiExt.RECT(), "uptr"), _rc)
/**
* Retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
* [Learn more](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclientrect)
* @returns {RECT}
*/
GetClientRect() => (DllCall("GetClientRect", "ptr", this.hwnd, "ptr", _rc := GuiExt.RECT(), "uptr"), _rc)
/**
* Sets the attributes of a window. Specifically, it can set the color of the window's caption, text, and border.
* @param {integer} [titleText] Specifies the color of the caption text. Specifying `0xFFFFFFFF` will reset to the system's default caption text color.
* @param {integer} [titleBackground] Specifies the color of the caption. Specifying `0xFFFFFFFF` will reset to the system's default caption color.
* @param {integer} [border] Specifies the color of the window border.
* - Specifying `0xFFFFFFFE` will suppress the drawing of the window border.
* - Specifying `0xFFFFFFFF` will reset to the system's default border color.
* The application is responsible for changing the border color in response to state changes, such as window activation.
* @since This is supported starting with Windows 11 Build 22000.
* @returns {String} - The result of the attribute setting operation.
*/
SetWindowColor(titleText?, titleBackground?, border?)
{
static DWMWA_BORDER_COLOR := 34
static DWMWA_CAPTION_COLOR := 35
static DWMWA_TEXT_COLOR := 36
static SetClrMap := Map(DWMWA_BORDER_COLOR, "border", DWMWA_CAPTION_COLOR, "titleBackground", DWMWA_TEXT_COLOR, "titleText")
if (VerCompare(A_OSVersion, "10.0.22200") < 0)
throw OSError("This is supported starting with Windows 11 Build 22000.")
for attr, var in SetClrMap
if (%var%??0)
this.SetWindowAttribute(attr, RgbToBgr(%var% is string && !InStr(%var%, "0x") ? Number("0x" %var%) : %var%))
RgbToBgr(color) => (((Color >> 16) & 0xFF) | (Color & 0x00FF00) | ((Color & 0xFF) << 16))
}
/**
* Calls the DwmSetWindowAttribute function from the dwmapi library to set attributes of a window.
* @param {number} dwAttribute - The attribute constant to set.
* @param {number} [pvAttribute] - The value of the attribute to set. Optional parameter.
* @returns {number} The result of the DllCall, typically indicating success or failure.
* @see [MSDN](https://learn.microsoft.com/en-us/windows/win32/api/dwmapi/nf-dwmapi-dwmsetwindowattribute)
*/
SetWindowAttribute(dwAttribute, pvAttribute?) => DllCall("dwmapi\DwmSetWindowAttribute", 'ptr', this.Hwnd, "uint", dwAttribute, "uint*", pvAttribute, "int", 4)
/**
* Sets the dark mode title bar for the window if the operating system version supports it.
* @returns {number|undefined} The result of setting the window attribute, or undefined if not applicable.
*/
SetDarkTitle()
{
if (attr := ((VerCompare(A_OSVersion, "10.0.18985") >= 0) ? 20 : (VerCompare(A_OSVersion, "10.0.17763") >= 0) ? 19 : 0))
return this.SetWindowAttribute(attr, true)
}
; Apply dark theme to all the context menus that is created by this script.
SetDarkMenu()
{
uxtheme := DllCall("GetModuleHandle", "ptr", StrPtr("uxtheme"), "ptr")
SetPreferredAppMode := DllCall("GetProcAddress", "ptr", uxtheme, "ptr", 135, "ptr")
FlushMenuThemes := DllCall("GetProcAddress", "ptr", uxtheme, "ptr", 136, "ptr")
DllCall(SetPreferredAppMode, "int", 1)
DllCall(FlushMenuThemes)
}
/**
* Send the message to the window or control, and then wait for confirmation.
* @param {Integer} Msg
* @param {Integer} [wParam=0]
* @param {Integer} [lParam=0]
* @returns {Integer}
*/
SendMsg(Msg, wParam := 0, lParam := 0) {
return SendMessage(Msg, wParam?, lParam?,, this)
}
/**
* Registers a function or method to be called whenever the Gui receives the specified message.
* @param {Integer} Msg The number of the message to monitor, which should be between 0 and 4294967295 (0xFFFFFFFF).
* @param {String|(GuiObj, wParam, lParam, Msg) => Integer} Callback The function, method or object to call when the event is raised.
* If the GUI has an event sink (that is, if Gui()'s EventObj parameter was specified), this parameter may be the name of a method belonging to the event sink.
* Otherwise, this parameter must be a function object. (**ahk_h 2.0**)The function may also consult the built-in variable `A_EventInfo`, which contains 0 if the message was sent via SendMessage.
* If sent via PostMessage, it contains the tick-count time the message was posted.
* @param {Integer} MaxThreads This integer is usually omitted. In this case, the monitoring function can only process one thread at a time. This is usually the best, because otherwise whenever the monitoring function is interrupted, the script will process the messages in chronological order. Therefore, as an alternative to MaxThreads, Critical can be considered, as shown below.
*
* Specify 0 to unregister the function previously identified by Function.
*
* By default, when multiple functions are registered for a MsgNumber, they will be called in the order of registration. To register a function before the previously registered function, specify a negative value for MaxThreads. For example, OnMessage Msg, Fn, -2 Register Fn to be called before any other functions registered for Msg, and allow Fn to have up to 2 threads. However, if the function has already been registered, the order will not change unless the registration is cancelled and then re-registered.
*/
OnMessage(Msg, Callback, MaxThreads?)
{
OnMessage(Msg, _callback, MaxThreads?)
super.OnEvent("Close", g => OnMessage(Msg, _callback, 0))
_callback(wParam, lParam, uMsg, hWnd) {
try if (uMsg = Msg && hwnd = this.hwnd)
return Callback(this, wParam, lParam, uMsg)
}
}
class Control extends Gui.Control
{
static __New(p := this.Prototype, sp?)
{
sp := sp ?? super.Prototype
for prop in p.OwnProps()
if (!sp.HasMethod(prop) && !InStr(prop, "__"))
sp.DefineProp(prop, p.GetOwnPropDesc(prop))
if sp.HasMethod("OnMessage")
p.DeleteProp("OnMessage")
}
/**
* @property {Integer} X X position
* @property {Integer} Y Y position
* @property {Integer} W Width
* @property {Integer} H Height
*/
X := unset, Y := unset, W := unset, H := unset
/**
* Retrieves the dimensions of the bounding rectangle of the specified window. The dimensions are given in screen coordinates that are relative to the upper-left corner of the screen.
* [Learn more](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclientrect)
* @returns {RECT}
*/
GetWindowRect() => (DllCall("GetWindowRect", "ptr", this.hwnd, "ptr", _rc := GuiExt.RECT(), "uptr"), _rc)
/**
* Retrieves the coordinates of a window's client area. The client coordinates specify the upper-left and lower-right corners of the client area. Because client coordinates are relative to the upper-left corner of a window's client area, the coordinates of the upper-left corner are (0,0).
* [Learn more](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getclientrect)
* @returns {RECT}
*/
GetClientRect() => (DllCall("GetClientRect", "ptr", this.hwnd, "ptr", _rc := GuiExt.RECT(), "uptr"), _rc)
/**
* Registers a function or method to be called whenever the GuiControl receives the specified message.
* @param {Integer} Msg The number of the message to monitor, which should be between 0 and 4294967295 (0xFFFFFFFF).
* @param {String|(GuiCtrlObj, wParam, lParam, Msg) => Integer} Callback The function, method or object to call when the event is raised.
* If the GUI has an event sink (that is, if Gui()'s EventObj parameter was specified), this parameter may be the name of a method belonging to the event sink.
* Otherwise, this parameter must be a function object. The function may also consult the built-in variable `A_EventInfo`, which contains 0 if the message was sent via SendMessage.
* If sent via PostMessage, it contains the tick-count time the message was posted.
* @param {Integer} AddRemove If omitted, it defaults to 1 (call the callback after any previously registered callbacks). Otherwise, specify one of the following numbers:
* - 1 = Call the callback after any previously registered callbacks.
* - -1 = Call the callback before any previously registered callbacks.
* - 0 = Do not call the callback.
*/
OnMessage(Msg, Callback, AddRemove := 1)
{
static SubClasses := Map()
static HookedMsgs := Map()
if !SubClasses.Has(this.hwnd) {
SubClasses[this.hwnd] := CallbackCreate(SubClassProc,, 6)
HookedMsgs[this.hwnd] := Map(Msg, Callback.Bind(this))
SetWindowSubclass(this, SubClasses[this.hwnd])
OnExit(RemoveWindowSubclass)
this.Gui.OnEvent("Close", RemoveWindowSubclass)
}
hm := HookedMsgs[this.hwnd]
if AddRemove
hm[Msg] := Callback.Bind(this)
else if hm.Has(Msg)
hm.Delete(Msg)
SubClassProc(hWnd?, uMsg?, wParam?, lParam?, uIdSubclass?, dwRefData?)
{
if HookedMsgs.Has(uIdSubclass) && HookedMsgs[uIdSubclass].Has(uMsg) {
reply := HookedMsgs[uIdSubclass][uMsg](wParam?, lParam?, uMsg?)
if IsSet(reply)
return reply
}
return DefSubclassProc(hwnd, uMsg?, wParam?, lParam?)
}
DefSubclassProc(hwnd?, uMsg?, wParam?, lParam?) => DllCall("DefSubclassProc", "Ptr", hwnd, "UInt", uMsg, "Ptr", wParam, "Ptr", lParam, "Ptr")
SetWindowSubclass(obj, cb) => DllCall("SetWindowSubclass", "Ptr", obj.hwnd, "Ptr", cb, "Ptr", obj.hwnd, "Ptr", 0)
RemoveWindowSubclass(*)
{
DetectHiddenWindows true
for hwnd, cb in SubClasses.Clone() {
try if WinExist(hwnd) {
DllCall("RemoveWindowSubclass", "Ptr", hWnd, "Ptr", cb, "Ptr", hWnd)
CallbackFree(cb)
}
SubClasses.Delete(hwnd)
}
OnExit(RemoveWindowSubclass, 0)
}
}
/**
* Applies a specified theme to the window through the SetWindowTheme function from the uxtheme library.
* @param {string} pszSubAppName - The name of the application's subcomponent to apply the theme to.
* @param {string} [pszSubIdList] - A semicolon-separated list of class names to apply the theme to. Optional parameter.
* @returns {boolean} True if the theme was set successfully, false otherwise.
* @link https://learn.microsoft.com/en-us/windows/win32/api/uxtheme/nf-uxtheme-setwindowtheme
*/
SetTheme(pszSubAppName, pszSubIdList := "") => (!DllCall("uxtheme\SetWindowTheme", "ptr", this.hwnd, "ptr", StrPtr(pszSubAppName), "ptr", pszSubIdList ? StrPtr(pszSubIdList) : 0) ? true : false)
/**
* Set the control's border style to rounded corners.
* @param {Integer} [corner=9] The radius of the rounded corners.
* @returns {void}
*/
SetRounded(corner := 9)
{
static WM_SIZE := 0x0005
SIZING(this)
this.OnMessage(WM_SIZE, SIZING)
SIZING(ctrl, wParam?, lParam?, msg?)
{
ctrl.Opt("+0x4000000")
rc := ctrl.GetClientRect()
rcRgn := DllCall('Gdi32\CreateRoundRectRgn', 'int', rc.left + 3, 'int', rc.top + 3, 'int', rc.right - 3, 'int', rc.bottom - 3, 'int', corner, 'int', corner, 'ptr')
DllCall("SetWindowRgn", "ptr", ctrl.hWnd, "ptr", rcRgn, "int", 1, "int")
ctrl.Redraw()
DllCall('Gdi32\DeleteObject', 'ptr', rcRgn, 'int')
}
}
/**
* Send the message to the window or control, and then wait for confirmation.
* @param {Integer} Msg
* @param {Integer} [wParam=0]
* @param {Integer} [lParam=0]
* @returns {Integer}
*/
SendMsg(Msg, wParam := 0, lParam := 0) => (SendMessage(Msg, wParam?, lParam?, this))
}
;;{ Gui.Addxxx methods:
/**
* Create a text control that the user cannot edit. Often used to label other controls.
* @param Options V: Sets the control's Name.
* Pos: xn yn wn hn rn Right Left Center Section
* VScroll HScroll -Tabstop -Wrap
* BackgroundColor BackgroundTrans
* Border Theme Disabled Hidden
* @param Text The text
* @returns {GuiExt.Control|GuiExt.Text}
*/
AddText(Options?, Text?) => super.AddText(Options?, Text?)
/**
* Create controls such as text, buttons or checkboxes, and return a GuiControl object.
* @param Options Limit Lowercase Multi Number Password ReadOnly
* Tn Uppercase WantCtrlA WantReturn WantTab
* V: Sets the control's Name.
* Pos: xn yn wn hn rn Right Left Center Section
* VScroll HScroll -Tabstop -Wrap
* BackgroundColor Border Theme Disabled Hidden
* @param Text The text in the Edit
* @returns {GuiExt.Control|GuiExt.Edit|Gui.Edit}
*/
AddEdit(Options?, Text?) => super.AddEdit(Options?, Text?)
/**
* Create UpDown control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.UpDown|Gui.UpDown}
*/
AddUpDown(Options?, Text?) => super.AddUpDown(Options?, Text?)
/**
* Create Picture control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Picture|Gui.Picture}
*/
AddPicture(Options?, FileName?) => super.AddPicture(Options?, FileName?)
/**
* Create Picture control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Picture|Gui.Picture}
*/
AddPic(Options?, FileName?) => super.AddPicture(Options?, FileName?)
/**
* Adds a Button control and returns a GuiControl object.
* @param Options Positioning and Sizing of Controls
* V: Sets the control's Name.
* Positioning: xn yn wn hn rn Right Left Center Section -Tabstop -Wrap
* BackgroundColor Border Theme Disabled Hidden
* @param Text The text of the button
* @returns {GuiEx.Control}
*/
AddButton(Options?, Text?) => super.AddButton(Options?, Text?)
/**
* Create Checkbox and return a GuiControl object.
* GuiCtrl.Value returns the number 1 for checked, 0 for unchecked, and -1 for gray/indeterminate.
* @param Options V: Sets the control's Name.
* Checked: Start off checked
* Check3: Enable a third "indeterminate" state that displays a gray checkmark
* CheckedGray: Start off checked or indeterminate
* CheckedN: Set state: 0, 1 or -1
* Pos: xn yn wn Right Left Center Section
* VScroll HScroll -Tabstop -Wrap
* BackgroundColor BackgroundTrans
* Border Theme Disabled Hidden
* @param Text The text of the Checkbox
* @returns {GuiExt.Control|GuiExt.Checkbox|Gui.Checkbox}
*/
AddCheckbox(Options?, Text?) => super.AddCheckbox(Options?, Text?)
/**
* Create Radio control and return a GuiControl object.
* GuiCtrl.Value returns the number 1 for checked, 0 for unchecked, and -1 for gray/indeterminate.
* Events: DoubleClick, Focus & LoseFocus
* @param Options V: Sets the control's Name.
* Checked: Start off checked
* CheckedN: Set state: 0 or 1
* Group: Start a new group
* Pos: xn yn wn Right Left Center Section
* VScroll HScroll -Tabstop -Wrap
* BackgroundColor BackgroundTrans
* Border Theme Disabled Hidden
* @param Text The text of the Checkbox
* @returns {GuiExt.Control|GuiExt.Radio|Gui.Radio}
*/
AddRadio(Options?, Text?) => super.AddRadio(Options?, Text?)
/**
* Create DropDownList control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.DropDownList|Gui.DropDownList}
*/
AddDropDownList(Options?, Items?) => super.AddDropDownList(Options?, Items?)
/**
* Create DropDownList control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.DropDownList|Gui.DropDownList}
*/
AddDDL(Options?, Items?) => super.AddDropDownList(Options?, Items?)
/**
* Create ComboBox control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.ComboBox|Gui.ComboBox}
*/
AddComboBox(Options?, Items?) => super.AddComboBox(Options?, Items?)
/**
* Create ListBox control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.ListBox|Gui.ListBox}
*/
AddListBox(Options?, Items?) => super.AddListBox(Options?, Items?)
/**
* Create ListView control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.ListView|Gui.ListView}
*/
AddListView(Options?, Titles?) => super.AddListView(Options?, Titles?)
/**
* Create TreeView control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.TreeView|Gui.TreeView}
*/
AddTreeView(Options?, Text?) => super.AddTreeView(Options?, Text?)
/**
* Create Link control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Link|Gui.Link}
*/
AddLink(Options?, Text?) => super.AddLink(Options?, Text?)
/**
* Create Hotkey control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Hotkey|Gui.Hotkey}
*/
AddHotkey(Options?, Text?) => super.AddHotkey(Options?, Text?)
/**
* Create DateTime control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.DateTime|Gui.DateTime}
*/
AddDateTime(Options?, DateTime?) => super.AddDateTime(Options?, DateTime?)
/**
* Create MonthCal control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.MonthCal|Gui.MonthCal}
*/
AddMonthCal(Options?, YYYYMMDD?) => super.AddMonthCal(Options?, YYYYMMDD?)
/**
* Create Slider control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Slider|Gui.Slider}
*/
AddSlider(Options?, Value?) => super.AddSlider(Options?, Value?)
/**
* Create Progress control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Progress|Gui.Progress}
*/
AddProgress(Options?, Value?) => super.AddProgress(Options?, Value?)
/**
* Create GroupBox control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.GroupBox|Gui.GroupBox}
*/
AddGroupBox(Options?, Text?) => super.AddGroupBox(Options?, Text?)
/**
* Create Tab control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Tab|Gui.Tab}
*/
AddTab(Options?, Pages?) => super.AddTab(Options?, Pages?)
/**
* Create Tab2 control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Tab2|Gui.Tab2}
*/
AddTab2(Options?, Pages?) => super.AddTab2(Options?, Pages?)
/**
* Create Tab3 control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Tab3|Gui.Tab3}
*/
AddTab3(Options?, Pages?) => super.AddTab3(Options?, Pages?)
/**
* Create StatusBar control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.StatusBar|Gui.StatusBar}
*/
AddStatusBar(Options?, Text?) => super.AddStatusBar(Options?, Text?)
/**
* Create ActiveX control and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.ActiveX|Gui.ActiveX}
*/
AddActiveX(Options?, Component?) => super.AddActiveX(Options?, Component?)
/**
* Create Custom controls and return a GuiControl object.
* @returns {GuiExt.Control|GuiExt.Custom|Gui.Custom}
*/
AddCustom(Win32Class?, Text?) => super.AddCustom(Win32Class?, Text?)
;;}
}
;; Examples
if !A_IsCompiled && A_LineFile = A_ScriptFullPath
Example_GuiExt()
Example_GuiExt()
{
static WM_LBUTTONDOWN := 0x0201
static WM_SETCURSOR := 0x0020
static WM_NCLBUTTONDOWN := 0x00A1
static EN_KILLFOCUS := 0x0200
static WM_SIZING := 0x0214
static WM_MOVE := 0x0003
static WM_MOVING := 0x0216
/**
* ### To ensure proper functioning of VSCode's Intelligence, you can:
* 1. Replace `Gui` object with `GuiExt`. (Recommended)
* 2. Annotate the variable type as GuiExt above the line where you create a new Gui object instance.(Like the example below:)
* @var {GuiExt} myGui
* */
myGui := Gui("-Caption +Resize")
; myGui := GuiExt("-Caption +Resize")
myGui.SetFont("cWhite s16", "Segoe UI")
myGui.BackColor := 0x202020
myGui.OnEvent("Size", Size)
myGui.OnMessage(WM_MOVING, (*) => myEdit.UpdatePos())
myGui.OnEvent("Escape", (*) => ExitApp())
; Registers a function or method to be called whenever the Gui or GuiControl receives the specified message. [Check out the official document for more information.](https://www.autohotkey.com/docs/alpha/lib/GuiOnMessage.htm)
myGui.OnMessage(WM_LBUTTONDOWN, DragWindow)
; Set Dark Titlebar
myGui.SetDarkTitle()
; Set Dark ContextMenu
myGui.SetDarkMenu()
if (VerCompare(A_OSVersion, "10.0.22000") >= 0)
{
; Set Rounded Window (Requires win 11)
myGui.SetWindowAttribute(33, 2)
; Remove the window border. (Requires win 11)
; Do not set this if you're creating a borderless window with `SetBorderless` method.
; myGui.SetWindowColor(, , -1)
; Set Mica (Alt) background. (Requires win 11 build 22600)
; [Learn more](https://learn.microsoft.com/en-us/windows/apps/design/style/mica#app-layering-with-mica-alt)
if (VerCompare(A_OSVersion, "10.0.22600") >= 0)
myGui.SetWindowAttribute(38, 4)
}
; Set the borderless
myGui.SetBorderless(6, BorderlessCallback, 500, 500, 500, 500)
; Gui Control objects created in this way do not work with VSCode's IntelliSense. Create an Edit control as shown below.
text := myGui.Add("Text", "Backgroundcaa2031 cwhite Center R1.5 0x200 w280", "Rounded Text Control")
; Set Rounded Control
text.SetRounded()
myEdit := myGui.Add("Edit", "vEdit -WantReturn -TabStop w300 h150 -E0x200 -HScroll -VScroll +Multi +ReadOnly cwhite Background" myGui.BackColor)
myEdit.SetFont(, "Consolas")
; Set Edit Control theme
myEdit.SetTheme("DarkMode_Explorer")
myEdit.UpdatePos := (ctrl => (ctrl.Value :=
(
"x: " myGui.X "
y: " myGui.Y "
w: " myGui.W "
h: " myGui.H
)))
; Hide the blinking caret
myEdit.OnEvent("Focus", (gCtrl, *) => (DllCall("HideCaret", "ptr", gCtrl.hWnd, "int"), gCtrl.SendMsg(EN_KILLFOCUS)))
myEdit.OnMessage(WM_SETCURSOR, SetCursor)
myGui.Show("w300 AutoSize")
myGui.Opt("MinSize")
; Send Message to the gui or gui control
myEdit.SendMsg(EN_KILLFOCUS)
WinRedraw(myGui)
WinWaitClose(myGui)
BorderlessCallback(g, x, y) {
if !g["Edit"]
return
WinGetPos(, &eY,,, g["Edit"])
return y <= eY
}
/**
* @param {GuiExt|Gui} GuiObj
* @param {Integer} MinMax
* @param {Integer} Width
* @param {Integer} Height
*/
Size(GuiObj, MinMax, Width, Height)
{
Critical("Off")
SetWinDelay(-1), SetControlDelay(-1)
; Moving Controls
myEdit.W := text.W := Width - (GuiObj.MarginX*2)
myEdit.H := Height - (GuiObj.MarginY*2)
myEdit.UpdatePos()
}
/**
* Callback function for `GuiCtrl.OnMessage()` [Check out the official document for more information.](https://www.autohotkey.com/docs/alpha/lib/GuiOnMessage.htm)
* @param GuiCtrlObj
* @param wParam
* @param lParam
* @param msg
* @returns {Integer}
*/
DragWindow(GuiCtrlObj, wParam, lParam, msg) {
static WM_NCLBUTTONDOWN := 0x00A1
PostMessage(WM_NCLBUTTONDOWN, 2,,, GuiCtrlObj is Gui.Control ? GuiCtrlObj.Gui : GuiCtrlObj)
return 0
}
/**
* Callback function for `GuiCtrl.OnMessage()` [Check out the official document for more information.](https://www.autohotkey.com/docs/alpha/lib/GuiOnMessage.htm)
* @param GuiCtrlObj
* @param wParam
* @param lParam
* @param msg
* @returns {Integer}
*/
SetCursor(GuiCtrlObj, wParam, lParam, Msg) {
static hCursor := DllCall("LoadCursor", "ptr", 0, "ptr", 32512)
DllCall("SetCursor", "ptr", hCursor, "ptr")
return 0
}
}
/*
Example_SetBorderless()
{
myGui := GuiExt("-Caption +Resize")
myGui.SetFont("cWhite s16", "Segoe UI")
myGui.SetDarkTitle()
myGui.SetDarkMenu()
myGui.BackColor := 0x202020
text := myGui.AddText("vTitlebar Backgroundcaa2031 cwhite Center R1.5 0x200 w280", "Titlebar Area")
text.Base := GuiExt.Control
text.SetRounded()
myGui.OnEvent('Size', Size)
;Set Mica (Alt) background. (Supported starting with Windows 11 Build 22000.)
if (VerCompare(A_OSVersion, "10.0.22600") >= 0)
myGui.SetWindowAttribute(38, 4)
myGui.SetBorderless(6, (g, x, y) => (y <= g['Titlebar'].GetWindowRect().bottom), 500, 500, 500, 500)
myGui.Show("h500")
Size(g, minmax, width, height)
{
SetControlDelay(-1)
; Set titlebar's width to fix the gui.
g["Titlebar"].W := (width - (g.MarginX*2))
}
}