-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathstdWindow.cls
2882 lines (2624 loc) · 121 KB
/
stdWindow.cls
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
VERSION 1.0 CLASS
BEGIN
MultiUse = -1 'True
END
Attribute VB_Name = "stdWindow"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
'@class stdWindow
'@description A class for managing windows
'@example:
' With stdWindow.CreateFromDesktop()
' Dim notepad as stdWindow
' set notepad = .Find(stdLambda.Create("$1.Caption = ""Untitled - Notepad"" and $1.ProcessName = ""notepad.exe"""))
' nodepad.SendKeysInput("hello world")
' nodepad.SendKeysInput("^a")
' nodepad.SendKeysInput("^c")
' Debug.Print stdClipboard.Text
' End With
'
' 'Make a userform resizable
' MyForm.show
' stdWindow.CreateFromIUnknown(MyForm).resizable = true
'
'Spec:
' CONSTRUCTORS
' [ ] Create(sClassName,sCaption,dwStyle, x, y, Width, Height, hWndParent, hMenu, hInstance, lpParam) as stdWindow
' [ ] TODO:CreateStaticPopup(x, y, Width, Height, BorderWidth, BorderColor) as stdWindow
' [X] CreateFromDesktop() as stdWindow
' [X] CreateFromHwnd(hwnd) as stdWindow
' [X] CreateFromPoint(x, y) as stdWindow
' [ ] CreateFromEvent() as stdWindow
' [X] CreateFromIUnknown(obj) as stdWindow
' [X] CreateFromContextMenu() as stdWindow 'Class == "#32768"
' STATIC METHODS
' [?] Requires()
' INSTANCE PROPERTIES
' [X] Get handle() as LongPtr
' [X] Get hDC() as LongPtr
' [X] Get Exists as Boolean
' [X] Get/Let Visible() as Boolean
' [X] Get/Let State() as EWndState 'Normal,Minimised,Maximised
' [X] Get IsFrozen() as Boolean
' [X] Get/Let Caption() as string
' [X] Get Class() as string
' [X] Get RectClient() as Long()
' [X] Get/Let RectWindow() as Long()
' [X] Get/Let X() as Long
' [X] Get/Let Y() as Long
' [X] Get/Let Width() as Long
' [X] Get/Let Height() as Long
' [X] Get ProcessID() as long
' [X] Get ProcessName() as string
' [X] Get/Set Parent() as stdWindow
' [X] Get AncestralRoot() as stdWindow
' [X] Get/Let Style() as Long
' [X] Get/Let StyleEx() as Long
' [X] Get/Let UserData() as LongPtr
' [X] Get/Let WndProc() as LongPtr
' [X] Get/Let Resizable() as Boolean
' [X] Get Children() as Collection
' [X] Get Descendents() as Collection
' [X] Get/Let isTopmost() as Boolean 'AlwaysOnTop
'
' INSTANCE METHODS
' [ ] SetHook(idHook, hook, hInstance, dwThreadID) as LongPtr
' [X] Redraw()
' [X] SendMessage(wMsg, wParam, lParam)
' [X] PostMessage(wMsg, wParam, lParam)
' [ ] TODO: SendMessageTimeout(wMsg, wParam, lParam, TimeoutMilliseconds)
' [ ] ClickInput(x?, y?, Button?)
' [X] ClickEvent(x?, y?, Button?, isDoubleClick?, wParam?)
' [ ] SendKeysInput(sKeys, bRaw?, keyDelay?)
' [X] SendKeysEvent(sKeys, bRaw?, keyDelay?)
' [X] Activate()
' [X] Close()
' [X] FindFirst(query)
' [X] FindAll(query)
' [ ] Screenshot()
' PROTECTED METHODS
' [X] zProtGetNextDescendent(stack, DFS, Prev) as stdWindow
' GENERIC
' [ ] TODO: Mac compatibility
'--------------------------------------------------------------------------------
'Win API Declares
'--------------------------------------------------------------------------------
Private Type apiRect
left As Long
top As Long
right As Long
bottom As Long
End Type
Private Type apiWindowInfo
cbSize As Integer 'DWORD
rcWindow As apiRect 'RECT
rcClient As apiRect 'RECT
dwStyle As Integer 'DWORD
dwExStyle As Integer 'DWORD
dwWindowStatus As Integer 'DWORD
cxWindowBorders As Long 'UINT
cyWindowBorders As Long 'UINT
atomWindowType As Long 'ATOM
wCreatorVersion As Long 'WORD
End Type
Public Enum apiWindowHookType
WH_MSGFILTER = -1
WH_JOURNALRECORD = 0
WH_JOURNALPLAYBACK = 1
WH_KEYBOARD = 2
WH_GETMESSAGE = 3
WH_CALLWNDPROC = 4
WH_SYSMSGFILTER = 6
WH_MOUSE = 7
WH_SHELL = 10
WH_CALLWNDPROCRET = 12
WH_KEYBOARD_LL = 13
WH_MOUSE_LL = 14
WH_CBT = 5
WH_DEBUG = 9
WH_FOREGROUNDIDLE = 11
End Enum
Private Enum apiWindowMessage
WM_GETICON = &H7F
WM_SETICON = &H80
End Enum
Private Enum apiWindowIconType
ICON_SMALL = 0
ICON_BIG = 1
ICON_SMALL2 = 2 'Used by WM_GETICON only. Retrieves the small icon provided by the application. If the application does not provide one, the system uses the system-generated icon for that window.
End Enum
'https://www.autohotkey.com/docs_1.0/misc/Styles.htm
Public Enum EWndStyles
WS_BORDER = &H800000
WS_CAPTION = &HC00000
WS_CHILD = &H40000000
WS_CHILDWINDOW = &H40000000
WS_CLIPCHILDREN = &H2000000
WS_CLIPSIBLINGS = &H4000000
WS_DISABLED = &H8000000
WS_DLGFRAME = &H400000
WS_GROUP = &H20000
WS_HSCROLL = &H100000
WS_ICONIC = &H20000000
WS_MAXIMIZE = &H1000000
WS_MAXIMIZEBOX = &H10000
WS_MINIMIZE = &H20000000
WS_MINIMIZEBOX = &H20000
WS_OVERLAPPED = &H0
WS_POPUP = &H80000000
WS_SIZEBOX = &H40000
WS_SYSMENU = &H80000
WS_TABSTOP = &H10000
WS_THICKFRAME = &H40000
WS_TILED = &H0
WS_VISIBLE = &H10000000
WS_VSCROLL = &H200000
WS_OVERLAPPEDWINDOW = WS_OVERLAPPED Or WS_CAPTION Or WS_SYSMENU Or WS_THICKFRAME Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
WS_POPUPWINDOW = WS_POPUP Or WS_BORDER Or WS_SYSMENU
End Enum
Public Enum EWndExStyles
WS_EX_ACCEPTFILES = &H10
WS_EX_APPWINDOW = &H40000
WS_EX_CLIENTEDGE = &H200
WS_EX_COMPOSITED = &H2000000
WS_EX_CONTEXTHELP = &H400
WS_EX_CONTROLPARENT = &H10000
WS_EX_DLGMODALFRAME = &H1
WS_EX_LAYERED = &H80000
WS_EX_LAYOUTRTL = &H400000
WS_EX_LEFT = &H0
WS_EX_LEFTSCROLLBAR = &H4000
WS_EX_LTRREADING = &H0
WS_EX_MDICHILD = &H40
WS_EX_NOACTIVATE = &H8000000
WS_EX_NOINHERITLAYOUT = &H100000
WS_EX_NOPARENTNOTIFY = &H4
WS_EX_NOREDIRECTIONBITMAP = &H200000
WS_EX_RIGHT = &H1000
WS_EX_RIGHTSCROLLBAR = &H0
WS_EX_RTLREADING = &H2000
WS_EX_STATICEDGE = &H20000
WS_EX_TOOLWINDOW = &H80
WS_EX_TOPMOST = &H8
WS_EX_TRANSPARENT = &H20
WS_EX_WINDOWEDGE = &H100
WS_EX_OVERLAPPEDWINDOW = WS_EX_WINDOWEDGE Or WS_EX_CLIENTEDGE
WS_EX_PALETTEWINDOW = WS_EX_WINDOWEDGE Or WS_EX_TOOLWINDOW Or WS_EX_TOPMOST
End Enum
Public Enum LayeredWindowFlag
lwfChromaKey = &H1
lwfOpacity = &H2
End Enum
Public Enum EWndState
Normal
Maximised
Minimised
End Enum
Public Enum EWndRectType
RectTypeWindow
RectTypeClient
End Enum
Private Enum KeyState
tap
up
down
End Enum
Private Enum EVirtualKey
VK_LBUTTON = &H1: VK_RBUTTON = &H2: VK_CANCEL = &H3: VK_MBUTTON = &H4: VK_XBUTTON1 = &H5
VK_XBUTTON2 = &H6: VK_BACK = &H8: VK_TAB = &H9: VK_CLEAR = &HC: VK_RETURN = &HD
VK_SHIFT = &H10: VK_CONTROL = &H11: VK_ALT = &H12: VK_PAUSE = &H13: VK_CAPITAL = &H14
VK_KANA = &H15: VK_HANGUEL = &H15: VK_HANGUL = &H15: VK_IME_ON = &H16: VK_JUNJA = &H17
VK_FINAL = &H18: VK_HANJA = &H19: VK_KANJI = &H19: VK_IME_OFF = &H1A: VK_ESCAPE = &H1B
VK_CONVERT = &H1C: VK_NONCONVERT = &H1D: VK_ACCEPT = &H1E: VK_MODECHANGE = &H1F: VK_SPACE = &H20
VK_PRIOR = &H21: VK_NEXT = &H22: VK_END = &H23: VK_HOME = &H24: VK_LEFT = &H25
VK_UP = &H26: VK_RIGHT = &H27: VK_DOWN = &H28: VK_SELECT = &H29: VK_PRINT = &H2A
VK_EXECUTE = &H2B: VK_SNAPSHOT = &H2C: VK_INSERT = &H2D: VK_DELETE = &H2E: VK_HELP = &H2F
'Numbers
VK_0 = &H30: VK_1 = &H31: VK_2 = &H32: VK_3 = &H33: VK_4 = &H34
VK_5 = &H35: VK_6 = &H36: VK_7 = &H37: VK_8 = &H38: VK_9 = &H39
'Alphabet:
VK_A = &H41: VK_B = &H42: VK_C = &H43: VK_D = &H44: VK_E = &H45
VK_F = &H46: VK_G = &H47: VK_H = &H48: VK_I = &H49: VK_J = &H4A
VK_K = &H4B: VK_L = &H4C: VK_M = &H4D: VK_N = &H4E: VK_O = &H4F
VK_P = &H50: VK_Q = &H51: VK_R = &H52: VK_S = &H53: VK_T = &H54
VK_U = &H55: VK_V = &H56: VK_W = &H57: VK_X = &H58: VK_Y = &H59
VK_Z = &H5A:
VK_LWIN = &H5B: VK_RWIN = &H5C: VK_APPS = &H5D: VK_SLEEP = &H5F
'Numpad
VK_NUMPAD0 = &H60: VK_NUMPAD1 = &H61: VK_NUMPAD2 = &H62: VK_NUMPAD3 = &H63: VK_NUMPAD4 = &H64:
VK_NUMPAD5 = &H65: VK_NUMPAD6 = &H66: VK_NUMPAD7 = &H67: VK_NUMPAD8 = &H68: VK_NUMPAD9 = &H69:
VK_MULTIPLY = &H6A: VK_ADD = &H6B: VK_SEPARATOR = &H6C: VK_SUBTRACT = &H6D: VK_DECIMAL = &H6E:
VK_DIVIDE = &H6F:
'Function keys
VK_F1 = &H70: VK_F2 = &H71: VK_F3 = &H72: VK_F4 = &H73: VK_F5 = &H74:
VK_F6 = &H75: VK_F7 = &H76: VK_F8 = &H77: VK_F9 = &H78: VK_F10 = &H79:
VK_F11 = &H7A: VK_F12 = &H7B: VK_F13 = &H7C: VK_F14 = &H7D: VK_F15 = &H7E:
VK_F16 = &H7F: VK_F17 = &H80: VK_F18 = &H81: VK_F19 = &H82: VK_F20 = &H83:
VK_F21 = &H84: VK_F22 = &H85: VK_F23 = &H86: VK_F24 = &H87:
'Modifiers
VK_NUMLOCK = &H90: VK_SCROLL = &H91:
VK_LSHIFT = &HA0: VK_RSHIFT = &HA1:
VK_LCONTROL = &HA2: VK_RCONTROL = &HA3:
VK_LALT = &HA4: VK_RALT = &HA5:
'Media keys
VK_BROWSER_BACK = &HA6: VK_BROWSER_FORWARD = &HA7: VK_BROWSER_REFRESH = &HA8: VK_BROWSER_STOP = &HA9: VK_BROWSER_SEARCH = &HAA: VK_BROWSER_FAVORITES = &HAB: VK_BROWSER_HOME = &HAC:
VK_VOLUME_MUTE = &HAD: VK_VOLUME_DOWN = &HAE: VK_VOLUME_UP = &HAF:
VK_MEDIA_NEXT_TRACK = &HB0: VK_MEDIA_PREV_TRACK = &HB1: VK_MEDIA_STOP = &HB2: VK_MEDIA_PLAY_PAUSE = &HB3:
VK_LAUNCH_MAIL = &HB4: VK_LAUNCH_MEDIA_SELECT = &HB5: VK_LAUNCH_APP1 = &HB6: VK_LAUNCH_APP2 = &HB7:
VK_OEM_PLUS = &HBB: VK_OEM_COMMA = &HBC: VK_OEM_MINUS = &HBD: VK_OEM_PERIOD = &HBE: VK_OEM_CLEAR = &HFE:
VK_OEM_1 = &HBA: VK_OEM_2 = &HBF: VK_OEM_3 = &HC0: VK_OEM_4 = &HDB: VK_OEM_5 = &HDC: VK_OEM_6 = &HDD: VK_OEM_7 = &HDE: VK_OEM_8 = &HDF: VK_OEM_102 = &HE2:
VK_PROCESSKEY = &HE5: VK_PACKET = &HE7: VK_ATTN = &HF6: VK_CRSEL = &HF7: VK_EXSEL = &HF8: VK_EREOF = &HF9: VK_PLAY = &HFA: VK_ZOOM = &HFB: VK_NONAME = &HFC: VK_PA1 = &HFD
End Enum
'
Private Type KeyToken
wVirtualKey As EVirtualKey 'https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
wScanCode As Integer '
iKeyState As KeyState
iTimes As Long
End Type
'Used by SendInput() to send keys to window
Private Type KeyboardInputEx
wVk As Integer 'WORD
wScan As Integer 'WORD
dwFlags As Long 'DWORD
time As Long 'DWORD
#If VBA7 Then 'ULONG_PTR
dwExtraInfo As LongPtr
#Else
dwExtraInfo As Long
#End If
padding As Currency
End Type
Private Type KeyboardInput
InputType As Long 'DWORD = INPUT_KEYBOARD
ki As KeyboardInputEx
End Type
'========================================
'Or use EnumChildWindows
Private Enum apiWindowRelationship
GW_CHILD = 5
GW_ENABLEDPOPUP = 6
GW_HWNDFIRST = 0
GW_HWNDLAST = 1
GW_HWNDNEXT = 2
GW_HWNDPREV = 3
GW_OWNER = 4
End Enum
Private Enum EWndShowStyle
SW_HIDE = 0
SW_SHOWNORMAL = 1 'Shows/Restores + Activates
SW_SHOWMINIMIZED = 2 'Activates the window and displays it as a minimized window.
SW_MAXIMIZE = 3 'Maximize
SW_SHOWNOACTIVATE = 4 'Shows in most recent size + position but doesn't activate
SW_SHOW = 5 'Activate
SW_MINIMIZE = 6 'Minimize
SW_SHOWMINNOACTIVE = 7 'Minimize no activate
SW_SHOWNA = 8 'Show in current size and position, no activate
SW_RESTORE = 9 'Restore
SW_SHOWDEFAULT = 10 'Default window state at start of program
SW_FORCEMINIMIZE = 11
End Enum
Private Enum apiWindowLongType
GWL_WNDPROC = -4
GWL_HINSTANCE = -6
GWL_HWNDPARENT = -8
GWL_ID = -12
GWL_STYLE = -16
GWL_EXSTYLE = -20
GWL_USERDATA = -21
'If HWND is a dialog box
DWL_MSGRESULT = 0
'DWL_DLGPROC = DWLP_MSGRESULT + sizeof(LRESULT)
'DWL_USER = DWL_DLGPROC + sizeof(DLGPROC)
End Enum
Private Enum apiWindowAncestorType
GA_PARENT = 1
GA_ROOT = 2
GA_ROOTOWNER = 3
End Enum
Public Enum EWndMouseButton
LButton
RButton
MButton
End Enum
'Used while walking the Window tree. Can be used to toggle between a Breadth first search and a depth first search.
Public Enum EWndFindType
BreadthFirst = 0
DepthFirst = 1
End Enum
'Used while walking the Window tree. Can be used to discard entire trees of elements, to increase speed of walk algorithms.
Public Enum EWndFindResult
matchFound = 1 'Matched
MatchFoundSearchDescendents = 4 'Same as `ESearchResult.MatchFound`
NoMatchFound = 0 'Not found, continue searching descendents
NoMatchCancelSearch = 2 'Not found, cancel search
NoMatchSkipDescendents = 3 'Not found, don't search descendents
End Enum
Private Type tFindNode
initialised As Boolean
depth As Long
element As Object
End Type
Private Enum apiClassLongType
GCL_MENUNAME = -8
GCL_HBRBACKGROUND = -10
GCL_HCURSOR = -12
GCL_HICON = -14
GCL_HMODULE = -16
GCL_CBWNDEXTRA = -18
GCL_CBCLSEXTRA = -20
GCL_WNDPROC = -24
GCL_STYLE = -26
GCL_HICONSM = -34
End Enum
Private Type tGUID
lData1 As Long
nData2 As Integer
nData3 As Integer
abytData4(0 To 7) As Byte
End Type
#If VBA7 Then
'Constructors
Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As LongPtr
Private Declare PtrSafe Function WindowFromAccessibleObject Lib "oleacc" (ByVal pacc As IAccessible, ByRef pHwnd As LongPtr) As Long
Private Declare PtrSafe Function AccessibleObjectFromWindow Lib "oleacc" (ByVal hwnd As LongPtr, ByVal dwId As Long, riid As tGUID, ppvObject As Object) As Long
'Getting window data
Private Declare PtrSafe Function IsWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function IsHungAppWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function IsZoomed Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare PtrSafe Function GetClientRect Lib "user32" (ByVal hwnd As LongPtr, ByRef pRect As apiRect) As Long
Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, ByRef pRect As apiRect) As Long
Private Declare PtrSafe Function GetWindowInfo Lib "user32" (ByVal hwnd As LongPtr, ByRef pInf As apiWindowInfo) As Long
Private Declare PtrSafe Function GetParent Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function SetParent Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndParent As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindowDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function GetWindowLongA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType) As Long
Private Declare PtrSafe Function GetWindowLongPtrA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType) As LongPtr
Private Declare PtrSafe Function SetWindowLongA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType, ByVal dwNewLong As Long) As Long
Private Declare PtrSafe Function SetWindowLongPtrA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType, ByVal dwNewPtr As LongPtr) As Long
Private Declare PtrSafe Function GetClassLongPtrA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiClassLongType) As LongPtr
Private Declare PtrSafe Function GetAncestor Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowAncestorType) As LongPtr
Private Declare PtrSafe Function FindWindowExA Lib "user32" (ByVal hwnd As LongPtr, ByVal hwndChildAfter As LongPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As LongPtr
Private Declare PtrSafe Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hwndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long, ByVal flags As Long) As Long
Private Declare PtrSafe Function MoveWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long, ByVal bRepaint As Boolean) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
'Redrawing window, UpdateWindow can also be used but isn't as safe...
Private Declare PtrSafe Function RedrawWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal lprcUpdate As LongPtr, ByVal hrgnUpdate As LongPtr, ByVal flags As Long) As Long
'Get children / siblings / parent
Private Declare PtrSafe Function GetWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal wCmd As apiWindowRelationship) As LongPtr
'Get process related data
Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, ByRef ldpwProcessId As Long) As Long
Private Declare PtrSafe Function GetWindowModuleFileName Lib "user32" Alias "GetWindowModuleFileNameA" (ByVal hwnd As LongPtr, ByVal pszFileName As String, ByVal cchFileNameMax As Long) As Long
Private Declare PtrSafe Function GetCurrentThreadId Lib "Kernel32" () As Long
Private Declare PtrSafe Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
'Setting window data
Private Declare PtrSafe Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String) As Long
'Layered windows
Private Declare PtrSafe Function GetLayeredWindowAttributes Lib "user32" (ByVal hwnd As LongPtr, ByRef crKey As Long, ByRef bAlpha As Byte, ByRef dwFlags As Long) As Long
Private Declare PtrSafe Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As LongPtr, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
'Automating windows
Private Declare PtrSafe Function SendMessageA Lib "user32" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Declare PtrSafe Function PostMessageA Lib "user32" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As EWndShowStyle) As Long
Private Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function DestroyWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
'SendKeys
Private Declare PtrSafe Function MapVirtualKeyA Lib "user32" (ByVal uCode As Long, ByVal uMapType As Long) As Long
Private Declare PtrSafe Sub keybd_event Lib "user32" (ByVal bVK As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)
Private Declare PtrSafe Function SendInput Lib "user32" (ByVal cInputs As Long, ByRef pInput As KeyboardInput, ByVal cbSize As Integer) As Long
Private Declare PtrSafe Function GetMessageExtraInfo Lib "user32" () As LongPtr
Private Declare PtrSafe Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
'Create windows
Private Declare PtrSafe Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As LongPtr, ByVal hMenu As LongPtr, ByVal hInstance As LongPtr, lpParam As Any) As LongPtr
Private Declare PtrSafe Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare PtrSafe Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As LongPtr
Private Declare PtrSafe Function SelectObject Lib "gdi32" (ByVal hdc As LongPtr, ByVal hObject As LongPtr) As LongPtr
Private Declare PtrSafe Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As LongPtr
Private Declare PtrSafe Function Rectangle Lib "gdi32" (ByVal hdc As LongPtr, ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long) As LongPtr
Private Declare PtrSafe Function UpdateWindow Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
#Else
Private Enum LongPtr
[_]
End Enum
'Constructors
Private Declare Function GetDesktopWindow Lib "user32" () As LongPtr
Private Declare Function IUnknown_GetWindow Lib "shlwapi" Alias "#172" (ByVal pIUnk As IUnknown, ByVal hwnd As LongPtr) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal x As Long, ByVal y As Long) As LongPtr
Private Declare Function WindowFromAccessibleObject Lib "oleacc" (ByVal pacc As IAccessible, ByRef pHwnd As LongPtr) As Long
Private Declare Function AccessibleObjectFromWindow Lib "oleacc" (ByVal hwnd As LongPtr, ByVal dwId As Long, riid As tGUID, ppvObject As Object) As Long
'Getting window data
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare Function IsHungAppWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare Function IsZoomed Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As LongPtr, ByRef pRect As apiRect) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, ByRef pRect As apiRect) As Long
Private Declare Function GetWindowInfo Lib "user32" (ByVal hwnd As LongPtr, ByRef pInf As apiWindowInfo) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare Function SetParent Lib "user32" (ByVal hwnd As LongPtr, ByVal hWndParent As LongPtr) As LongPtr
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare Function GetWindowLongA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType) As Long
Private Declare Function GetWindowLongPtrA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType) As LongPtr
Private Declare Function SetWindowLongA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType, ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowLongPtrA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowLongType, ByVal dwNewPtr As LongPtr) As Long
Private Declare Function GetClassLongPtrA Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiClassLongType) As LongPtr
Private Declare Function GetAncestor Lib "user32" (ByVal hwnd As LongPtr, ByVal nIndex As apiWindowAncestorType) As LongPtr
Private Declare Function FindWindowExA Lib "user32" (ByVal hwnd As LongPtr, ByVal hwndChildAfter As LongPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As LongPtr
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As LongPtr, ByVal hwndInsertAfter As LongPtr, ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long, ByVal flags As Long) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long, ByVal bRepaint As Boolean) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
'Redrawing window, UpdateWindow can also be used but isn't as safe...
Private Declare Function RedrawWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal lprcUpdate As LongPtr, ByVal hrgnUpdate As LongPtr, ByVal flags As Long) As Long
'Get children / siblings / parent
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal wCmd As apiWindowRelationship) As LongPtr
'Get process related data
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, ByRef ldpwProcessId As Long) As Long
Private Declare Function GetWindowModuleFileName Lib "user32" Alias "GetWindowModuleFileNameA" (ByVal hwnd As LongPtr, ByVal pszFileName As String, ByVal cchFileNameMax As Long) As Long
Private Declare Function GetCurrentThreadId Lib "Kernel32" () As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
'Setting window data
Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As LongPtr, ByVal lpString As String) As Long
'Layered windows
Private Declare Function GetLayeredWindowAttributes Lib "user32" (ByVal hwnd As LongPtr, ByRef crKey As Long, ByRef bAlpha As Byte, ByRef dwFlags As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As LongPtr, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
'Automating windows
Private Declare Function SendMessageA Lib "user32" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Declare Function PostMessageA Lib "user32" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Any, ByVal lParam As Any) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As EWndShowStyle) As Long
Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare Function DestroyWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
'SendKeys
Private Declare Function MapVirtualKeyA Lib "user32" (ByVal uCode As Long, ByVal uMapType As Long) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVK As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As LongPtr)
Private Declare Function SendInput Lib "user32" (ByVal cInputs As Long, ByRef pInput As KeyboardInput, ByVal cbSize As Integer) As Long
Private Declare Function GetMessageExtraInfo Lib "user32" () As LongPtr
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
'Create windows
Private Declare Function CreateWindowEx Lib "user32" Alias "CreateWindowExA" (ByVal dwExStyle As Long, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hWndParent As LongPtr, ByVal hMenu As LongPtr, ByVal hInstance As LongPtr, lpParam As Any) As LongPtr
Private Declare Function GetDC Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As LongPtr
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As LongPtr, ByVal hObject As LongPtr) As LongPtr
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As LongPtr) As LongPtr
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As LongPtr, ByVal hdc As LongPtr) As LongPtr
Private Declare Function Rectangle Lib "gdi32" (ByVal hdc As LongPtr, ByVal x1 As Long, ByVal y1 As Long, ByVal x2 As Long, ByVal y2 As Long) As LongPtr
Private Declare Function UpdateWindow Lib "user32" (ByVal hwnd As LongPtr) As LongPtr
#End If
Private Type TLayeredWindowInfo
crKey As Long
bAlpha As Byte
End Type
Private Type TThis
Handle As LongPtr
Initialized As Boolean
LayeredWindowInfo As TLayeredWindowInfo
Lookups As Object
isCreatedByVBA as Boolean
End Type
Private This as TThis
'================================================================================================
'= PUBLIC CONSTRUCTORS
'================================================================================================
'Create a window and return a window object
'@constructor
'@param sClassName - The class name can be any name registered with RegisterClass or RegisterClassEx, provided that the module that registers the class is also the module that creates the window. The class name can also be any of the predefined system class names. For a list of system class names, see the Remarks section.
'@param sCaption - The name/caption of the window
'@param dwStyle - The window style for the window
'@param x - The x coordinate of the window
'@param y - The y coordinate of the window
'@param width - The width of the window
'@param height - The height of the window
'@param hWndParent - Parent window handle. Can be 0 for pop-up windows.
'@param hMenu - Menu handle. Can be 0 for pop-up windows.
'@param hInstance - Module Instance handle.
'@param lpParam - Pointer to a location where extra information is stored. Or ClientCreateStruct (for MDI windows), or null if no extra data required
'@returns - The created window
'@remarks System Class Names: `BUTTON`, `COMBOBOX`, `EDIT`, `LISTBOX`, `MDICLIENT`, `RICHEDIT`, `RICHEDIT_CLASS`, `SCROLLBAR`, `STATIC`
'@example ```vb
'Dim wnd as stdWindow: set wnd = stdWindow.Create("STATIC", 0, 0, 100, 100, "Hello World")
'wnd.visible = true
'```
#If VBA7 Then
Public Function Create(ByVal sClassName As String, ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long, Optional ByVal sCaption As String = vbNullString, Optional ByVal dwStyle As Long = WS_POPUP, Optional ByVal dwStyleEx As Long = 0, Optional ByVal hWndParent As LongPtr = 0, Optional ByVal hMenu As LongPtr = 0, Optional ByVal hInstance As LongPtr = 0, Optional ByVal lpParam As Long = 0) As stdWindow
#Else
Public Function Create(ByVal sClassName As String, ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long, Optional ByVal sCaption As String = vbNullString, Optional ByVal dwStyle As Long = WS_POPUP, Optional ByVal dwStyleEx As Long = 0, Optional ByVal hWndParent As Long = 0, Optional ByVal hMenu As Long = 0, Optional ByVal hInstance As Long = 0, Optional ByVal lpParam As Long = 0) As stdWindow
#End If
Dim hwnd as LongPtr: hwnd = CreateWindowEx(dwStyleEx, sClassName, sCaption, dwStyle, x, y, width, height, hWndParent, hMenu, hInstance, lpParam)
If hwnd = 0 Then Err.Raise 1, "Create", "Failed to create window."
Set Create = new stdWindow
Call Create.protInit(hwnd, true)
End Function
'Create a static popup window and return a window object
'@constructor
'@param x - The x coordinate of the window
'@param y - The y coordinate of the window
'@param width - The width of the window
'@param height - The height of the window
'@param BorderWidth - The width of the colored border
'@param BorderColor - The color of the colored border
'@returns - The created highlighting box
'@remarks See [Creating a transparent window](https://stackoverflow.com/questions/3970066/creating-a-transparent-window-in-c-win32)
Public Function CreateHighlightRect(ByVal x As Long, ByVal y As Long, ByVal width As Long, ByVal height As Long, Optional ByVal BorderWidth As Long = 5, Optional ByVal BorderColor As Long = 255) As stdWindow
set CreateHighlightRect = Create("STATIC", x, y, width, height)
CreateHighlightRect.transparentColor = RGB(255,255,255)
CreateHighlightRect.isClickThroughEnabled = true
CreateHighlightRect.Visible = True
Call UpdateWindow(CreateHighlightRect.Handle)
'Create highlighted border
Dim hDC as LongPtr: hDC = CreateHighlightRect.hDCClient
Dim hPen as LongPtr: hPen = CreatePen(0, BorderWidth, BorderColor)
SelectObject hDC, hPen
Rectangle hDC, 0, 0, width, height
SelectObject hDC, 0
DeleteObject hPen
Call ReleaseDC(CreateHighlightRect.Handle, hDC)
End Function
'Whenever object is destroyed, destroy window if it was created by VBA
Private Sub Class_Terminate()
if This.isCreatedByVBA then
if This.Handle <> 0 then Call DestroyWindow(This.Handle)
end if
End Sub
'Create a window object from an existing window handle
'@constructor
'@param hwnd - Handle to window (hwnd) to create window object for
'@returns - Specificed window.
#If VBA7 Then
Public Function CreateFromHwnd(ByVal hwnd As LongPtr) As stdWindow
#Else
Public Function CreateFromHwnd(ByVal hwnd As Long) As stdWindow
#End If
Set CreateFromHwnd = New stdWindow
Call CreateFromHwnd.protInit(hwnd)
End Function
'Creates an `stdWindow` object from the current running application (e.g. Excel / Word / Powerpoint).
'@constructor
'@param oApp - Optional. Application object to create window from. If not specified, the current running application is used.
'@returns - Window object representing the current running application
'@remarks - This function is only implemented for Excel, Word and PowerPoint. For other applications, find the window with `stdWindow.CreateFromDesktop()` and then use `desktop.children` to find the required window object.
'Alternatively you might be able to use CommandBars("Status Bar") for other applications, similar to that of PowerPoint, but this is untested.
Public Function CreateFromApplication(Optional ByVal oApp as Object = nothing) As stdWindow
if oApp is nothing then set oApp = Application
select case oApp.Name
case "Microsoft Excel"
Set CreateFromApplication = CreateFromHwnd(oApp.hwnd)
case "Microsoft Word"
Set CreateFromApplication = CreateFromHwnd(oApp.ActiveWindow.Hwnd)
case "Microsoft PowerPoint"
set CreateFromApplication = CreateFromIAccessible(oApp.CommandBars("Status Bar")).AncestralRoot
case "Microsoft Access"
set CreateFromApplication = CreateFromHwnd(oApp.hWndAccessApp)
Case "Outlook"
Set CreateFromApplication = CreateFromIAccessible(oApp.ActiveWindow.CommandBars("Status Bar")).AncestralRoot
Case "Microsoft Publisher"
Set CreateFromApplication = CreateFromIAccessible(oApp.CommandBars("Status Bar")).AncestralRoot
case else
Err.Raise 1, "stdAcc::CreateFromApplication()", "No implementation for getting application window of " & Application.name
end select
End Function
'Create a window from the desktop window
'@constructor
'@returns - Desktop window
Public Function CreateFromDesktop() As stdWindow
Set CreateFromDesktop = CreateFromHwnd(GetDesktopWindow())
End Function
'Find and Create a window object for a window displayed intersecting a point on the screen.
'@constructor
'@param x - X of Point to find window at.
'@param y - Y of Point to find window at.
'@returns - Window intersecting point.
Public Function CreateFromPoint(ByVal x As Long, ByVal y As Long) As stdWindow
Set CreateFromPoint = New stdWindow
Call CreateFromPoint.protInit(WindowFromPoint(x, y))
End Function
''Create from Shell event
'Public Function CreateFromEvent() as stdWindow
'
'End Function
'Create a window object from an IUnknown object.
'@constructor
'@param obj - Object which implements GetWindow() method
'@returns - Window specified by the object.
'@remark - Uses IUserWindow::GetWindow() to get the window handle. This is implemented by `IOleWindow`, `IInternetSecurityMgrSite` and `IShellView`.
'@remark - A `UserForm` is an example of an object which implements `IOleWindow`.
Public Function CreateFromIUnknown(ByVal obj As IUnknown) As stdWindow
Dim hwnd As LongPtr
Dim hResult As Long
hResult = IUnknown_GetWindow(obj, VarPtr(hwnd))
If hResult = 0 Then
Set CreateFromIUnknown = New stdWindow
Call CreateFromIUnknown.protInit(hwnd)
ElseIf hResult = -2147467262 Then
Err.Raise 1, "CreateFromIUnknown", "This object does not implement IOleWindow, IInternetSecurityMgrSite or IShellView, and thus cannot retrieve the window assosciated with the object."
Else
Err.Raise 1, "CreateFromIUnknown", "An unknown error has occurred.", hResult
End If
End Function
'Create a window object from an IAccessible object
'@constructor
'@param pAcc as IAccessible - Object which implements IAccessible
'@returns - Window specified by IAccessible object
Public Function CreateFromIAccessible(ByVal pAcc As IUnknown) As stdWindow
Dim hwnd As LongPtr
If WindowFromAccessibleObject(pAcc, hwnd) = 0 Then
Set CreateFromIAccessible = CreateFromHwnd(hwnd)
Else
Err.Raise 1, "CreateFromIAccessible", "Could not find window from IAccessible."
End If
End Function
'Create a window object from the context menu. There should only ever be 1 context menu on the system at one time.
'@constructor
'@returns - Window object representing the ContextMenu
Public Function CreateFromContextMenu() As stdWindow
Dim hwnd As LongPtr
hwnd = FindWindowExA(Null, Null, MakeIntAtom(&H8000&), vbNullString)
If hwnd <> 0 Then
Set CreateFromContextMenu = CreateFromHwnd(hwnd)
Else
Set CreateFromContextMenu = Nothing
End If
End Function
'Create window objects for all desktop windows which have a specified process ID
'@constructor
'@param processID - Process ID to find windows for
'@returns - Collection of visible windows for the process
'@remarks - Functionally equivalent of `stdWindow.CreateFromDesktop().FindAll(stdLambda.Create("$2.exists and $2.visible and $2.ProcessID = $1").bind(processID), iStaticDepthLimit:=1)`
Public Function CreateManyFromProcessId(ByVal processID As Long) As Collection
Dim ret As Collection: Set ret = New Collection
Dim win As stdWindow
For Each win In stdWindow.CreateFromDesktop().children
If win.exists and win.visible then
If win.processID = processID Then
ret.add win
End If
end if
Next
Set CreateManyFromProcessId = ret
End Function
'Create window objects for all shell windows
'@constructor
'@returns Collection<stdWindow> - Collection of shell windows
Public Function CreateManyFromShellWindows() as Collection
set CreateManyFromShellWindows = new Collection
Dim shell: set shell = CreateObject("Shell.Application")
Dim win as object
For each win in shell.windows
CreateManyFromShellWindows.add CreateFromHwnd(win.hwnd)
next
End Function
'Converts a stdWindow object to a stdAcc object
'@returns Object<stdAcc>|stdAcc - stdAcc object representing the window
'@remark - This function requires stdAcc dependency.
'@remark - For full intellisense, add `stdAccPresent = 1` to the compiler constants
#if stdAccPresent then
Public Function AsAcc() as stdAcc
#else
Public Function AsAcc() as Object
#end if
if isObject(stdAcc) then
set AsAcc = stdAcc.CreateFromHwnd(This.Handle)
else
Err.Raise 1, "", "This function requires stdAcc dependency."
end if
End Function
'Converts a stdWindow object to a IAccessible object
'@constructor
'@returns Object<IAccessible> - IAccessible object representing the window
Public Function AsIAccessible() As Object
Dim Guid As tGUID: Guid = convertGUID("618736E0-3C3D-11CF-810C-00AA00389B71")
Call AccessibleObjectFromWindow(This.handle, 0, Guid, AsIAccessible)
End Function
'Converts a stdWindow object to a IAccessible object
'@constructor
'@returns Object<IAccessible> - IAccessible object representing the window
'@example - `stdWindow.CreateFromHwnd(...).FindFirst(stdLambda.Create("$1.Class = ""EXCEL7""")).AsNativeObject.Application)`
Public Function AsNativeObject() As Object
Const OBJID_NATIVEOM = &HFFFFFFF0
Dim Guid As tGUID: Guid = convertGUID("00020400-0000-0000-C000-000000000046")
Call AccessibleObjectFromWindow(This.handle, OBJID_NATIVEOM, Guid, AsNativeObject)
End Function
'Notes:
'Windows are hierarchical therefore CreateManyFromQuery and CreateFromQuery makes less sense than FindFirst() and FindAll() methods
'================================================================================================
'= PROTECTED CONSTRUCTORS / DESTRUCTORS
'================================================================================================
'Initialize a window object
'@constructor
'@protected
'@param hwnd - Handle to window (hwnd) to create window object for
'@returns - Specificed window.
#If VBA7 Then
Friend Sub protInit(ByVal hwnd As LongPtr, Optional ByVal isCreatedByVBA as Boolean = false)
#Else
Friend Sub protInit(ByVal hwnd As Long, Optional ByVal isCreatedByVBA as Boolean = false)
#End If
This.Handle = hwnd
This.Initialized = True
This.isCreatedByVBA = isCreatedByVBA
End Sub
'Whenever we initialise ensure to get lookups from `stdWindow`
Private Sub Class_Initialize()
Set This.Lookups = stdWindow.protGetLookups()
End Sub
'--------------------------------------------------------------------------------
'Access window information
'--------------------------------------------------------------------------------
'Get the hWND / window ID of the window
'@returns - Window handle
#If VBA7 Then
Public Property Get handle() As LongPtr
#Else
Public Property Get handle() As Long
#End If
handle = This.Handle
End Property
'Get the handle to the display context for the window
'@returns - Window display context
#If VBA7 Then
Public Property Get hDC() As LongPtr
#Else
Public Property Get hDC() As Long
#End If
hDC = GetWindowDC(This.Handle)
End Property
'Get the handle to the display context for the window client
'@returns - Window display context
#If VBA7 Then
Public Property Get hDCClient() As LongPtr
#Else
Public Property Get hDCClient() As Long
#End If
hDCClient = GetDC(This.Handle)
End Property
'Detect if the window exists
'@returns - `true` if window exists, `false` otherwise
Public Property Get Exists() As Boolean
Exists = IsWindow(This.Handle)
End Property
'Detect if the window is hanging/frozen
'@returns - `true` if window is hanging/frozen, `false` otherwise
Public Property Get IsFrozen() As Boolean
If Exists Then
IsFrozen = IsHungAppWindow(This.Handle)
Else
Err.Raise 1, "IsFrozen", "Window does not exist."
End If
End Property
'Get/Set the window caption
'@returns - Window caption
Public Property Get Caption() As String
If Exists Then
Dim sCharBuffer As String, iNumChars As Long
sCharBuffer = space(256)
iNumChars = GetWindowText(This.Handle, sCharBuffer, 256)
Caption = Mid(sCharBuffer, 1, iNumChars)
Else
Err.Raise 1, "Caption", "Window does not exist."
End If
End Property
Public Property Let Caption(ByVal s As String)
If Exists Then
If Not CBool(SetWindowText(This.Handle, s)) Then
Err.Raise 1, "Caption [Let]", "Window text could not be set."
End If
Else
Err.Raise 1, "Caption [Let]", "Window does not exist."
End If
End Property
'Get the window class
'@returns - Window's win32 class
Public Property Get Class() As String
If Exists Then
Dim sCharBuffer As String, iNumChars As Long
sCharBuffer = space(256)
iNumChars = GetClassName(This.Handle, sCharBuffer, 256)
Class = Mid(sCharBuffer, 1, iNumChars)
Else
Err.Raise 1, "Class", "Window does not exist."
End If
End Property
'Get/Let visibility of window
'@returns - `true` if window is visible, `false` otherwise
Public Property Get Visible() As Boolean
If Exists Then
Visible = IsWindowVisible(This.Handle)
Else
Err.Raise 1, "Visible", "Window does not exist."
End If
End Property
Public Property Let Visible(ByVal x As Boolean)
If Exists Then
If x Then
Call ShowWindow(This.Handle, EWndShowStyle.SW_SHOWNOACTIVATE)
Else
Call ShowWindow(This.Handle, EWndShowStyle.SW_HIDE)
End If
Else
Err.Raise 1, "Visible", "Window does not exist."
End If
End Property
'Get/Let windowState of window
'@returns - Window state
Public Property Get State() As EWndState
If Exists Then
If IsZoomed(This.Handle) Then
State = EWndState.Maximised
ElseIf IsIconic(This.Handle) Then
State = EWndState.Minimised
Else
State = EWndState.Normal
End If
Else
Err.Raise 1, "State", "Window does not exist."
End If
End Property
Public Property Let State(ByVal x As EWndState)
If Exists Then
If Visible Then
Select Case x
Case EWndState.Normal
Call ShowWindow(This.Handle, EWndShowStyle.SW_RESTORE)
Case EWndState.Maximised
Call ShowWindow(This.Handle, EWndShowStyle.SW_MAXIMIZE)
Case EWndState.Minimised
Call ShowWindow(This.Handle, EWndShowStyle.SW_FORCEMINIMIZE)
End Select
Else
Err.Raise 1, "State", "Cannot set window state of a hidden window."
End If
Else
Err.Raise 1, "State", "Window does not exist."
End If
End Property
'Get the child windows of this window
'@returns Collection<stdWindow> - Collection of child windows
Public Property Get children() As collection
If Exists Then
'Define collection to return
Dim ret As collection
Set ret = New collection
'Attempt to get a child window
Dim childHandle As LongPtr
childHandle = GetWindow(This.Handle, GW_CHILD)
'If a child window exists, find all children
If childHandle <> 0 Then
'Quickly add all hwnds to an array
'This is a fast operation, object creation is anticipated to be a slower operation, thus is left till afterwards
Dim childHandles() As LongPtr
Dim i As Long: i = -1
Do While (childHandle <> 0)
i = i + 1
ReDim Preserve childHandles(i)
childHandles(i) = childHandle
childHandle = GetWindow(childHandle, GW_HWNDNEXT)