-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathclsWindow.cls
1946 lines (1765 loc) · 82.1 KB
/
clsWindow.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
Persistable = 0 'NotPersistable
DataBindingBehavior = 0 'vbNone
DataSourceBehavior = 0 'vbNone
MTSTransactionMode = 0 'NotAnMTSObject
END
Attribute VB_Name = "clsWindow"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'==============================================================================================
'名 称:windows窗体控制类v2.3
'描 述:一个操作windows窗口的类,可对窗口进行很多常用的操作(类名为clsWindow)
'使用范例:Dim window As New clsWindow
' window.GetWindowByTitle("计算器").closeWindow' ***!!!win10如果异常请用管理员权限执行***!!!
'编 程:sysdzw 原创开发,如果有需要对模块扩充或更新的话请邮箱发我一份,共同维护
'发布日期:2013/06/01
'博 客:https://blog.csdn.net/sysdzw
'用户手册:https://www.kancloud.cn/sysdzw/clswindow/
'Email :[email protected]
'QQ :171977759
'版 本:V1.0 初版 2012/12/03
' V1.1 修正了几个正则相关的函数,调整了部分类结构 2013/05/28
' V1.2 增加属性Caption,可以获取或设置当前标题栏 2013/05/29
' V1.3 增加了方法Focus,可以激活当前窗口 2013/06/01
' 增加了方法Left,Top,Width,Height,Move,处理窗口位置等
' V1.4 增加了窗口位置调整的几个函数 2013/06/04
' 增加了得到应用程序路径的函数AppName
' 增加了得到应用程序启动参数的函数AppCommandLine
' V1.5 增加了窗口最大最小化,隐藏显示正常的几个函数 2013/06/06
' 增加了获取控件相关函数是否使用正则的参数UseRegExp默认F
' V1.6 将Left,Top函数改为属性,可获得可设置 2013/06/10
' V1.7 增加函数:CloseApp 结束进程 2013/06/13
' 修正了部分跟正则匹配相关的函数
' 增加函数:GetElementTextByText
' 增加函数:GetElementHwndByText
' V1.8 增加函数:GetWindowByClassName 2013/06/26
' 增加函数:GetWindowByClassNameEx
' 增加函数:GetWindowByAppName
' 增加私有变量hWnd_
' 增加属性hWnd,可设置,单设置时候会检查,非法则设置为0
' 更新GetWindowByTitleEx函数,使之可以选择性支持正则
' 删除GetWindowByTitleRegExp函数,合并到上面函数
' 增加SetFocus函数,调用Focus实现,为了是兼容VB习惯
' 扩了ProcessID、AppPath、AppName、AppCommandLine三个函数,可带参数
' 网友wwb([email protected])提供了一些函数和方法属性:
' CheckWindow, Load, WindowState, Visible, hDC, ZOrder
' AlphaBlend, Enabled, Refresh, TransparentColor
' 采纳wwb网友的部分意见,将句柄变量改为hWnd_,但是hWnd作为公共属性
' V1.9 修正函数:GetMatchHwndFromWindow 正则表达式的错误 2013/08/07
' 修正函数:GetMatchHwndFromWindow 函数中的一些错误 2014/09/23
' 增加函数:GetWindowByClassNameEx
' 增加函数:GetWindowByPID 根据PID取窗口句柄
' 增加函数:GetCaptionByHwnd 根据句柄取得标题
' 增加函数:SetTop设置窗体置顶,传入参数false则取消 2014/09/24
' 增加函数:Shake、FadeIn、FadeOut 抖动、淡入、淡出特效
' V2.0 修正函数:GetWindowByPID 遍历窗体Win7下有一些问题 2015/09/29
' 修正函数:GetWindowByAppName 遍历窗体Win7下有一些问题
' 修正函数:GetWindowByAppNameEx 遍历窗体Win7下有一些问题
' V2.1 修正函数:ClickPoint 增加位置模式参数相对和绝对,默认相对 2018/06/05
' 增加函数:SelectComboBoxIndex 根据指定的index选择下拉框中的项
' 上述方法得到网友Chen8013的不少帮助,特此感谢
' 增加函数:GetWindowByHwnd 根据指定的句柄确定窗口 2018/07/22
' 增加函数:GetWindowByCursorPos 根据当前光标获取窗口(控件)
' 增加函数:GetWindowByPoint 根据指定的位置获取窗口(控件)
' 升级ClickPoint函数,支持点击前后分别延时,默认延时为0 2018/07/23
' V2.2 修正正则:网友小凡反应了句柄和id存在负数的情况 2020/01/08
' 优化属性:Caption(Get),根据网友小凡的建议改成可获得文本框内容
' 增加方法:Wait 此方法原为clsWaitableTimer模块中,现集成进来 2020/01/09
' 增加方法:ClickCurrentPoint 点击当前点 2020/01/10
' 增加方法:SetCursor(别名:SetPoint MoveCursor MoveCursorTo)
' 更新函数:将所有默认等待超时60秒的函数中默认等待时间都改为10秒
' 增加属性:Text、Value、Title(均为Caption别名) 2020/01/12
' 优化代码:GetCaptionByHwnd采用原Caption(Get)代码,后者也做了调整
' 增加函数:GetCursorPosCurrent(别名:GetCursorPoint)得到当前坐标
' 优化函数:所有窗口获取的函数增加了是否过滤可见的参数 2020/01/16
' 增加函数:GetTextByHwnd(同GetCaptionByHwnd)
' 优化代码结构。将模块中能移过来的都移到类模块中了 2020/01/19
' 增加函数:myIsWindowVisibled 判断窗体可见,长宽为0也认为不可见 2020/01/31
' 优化函数:GetTextByHwnd 网友小凡提供 2020/02/03
' 增加函数:CommandLine(同AppCommandLine) 2020/02/05
' 增加函数:MakeTransparent 设置窗口透明度 2020/02/18
' 增加函数:MoveToCenter 移动窗口到屏幕中心
' 增加函数:IsTopmost 判断窗口是否为置顶 2020/02/20
' 增加函数:GetWindowTextByHwnd 获得窗口标题,给窗口句柄专用 2020/02/28
' 修正函数:Focus 旧方法使用后会改变置顶窗口属性 2020/03/02
' 增加函数:IsWin64 网友小凡提供 2020/03/12
' 修正函数:AppPath 网友小凡提供兼容64位系统的方法
' 修正函数:AppCommandLine 网友小凡做了兼容64位处理及其他代码优化 2020/03/15
' 增加函数:IsForegroundWindow 判断窗口是否为活动窗口 2020/03/17
' 增加函数:GetClassNameByHwnd 根据句柄得到类名
' 增加属性:ClassName(Get) 返回窗口的类名
' 更新函数:CheckWindow 返回值由Long改成Boolean了,并且设为Public
' 增加函数:Click 点击当前句柄或者指定句柄 2020/06/29
' 更新函数:为兼容win10将设置窗口最大最小化改用SendMessage实现 2021/12/10
' 增加函数:Restore 还原窗口,比如最大最小化后需要还原
' V2.3 增加函数:SendKeys 替代VB自带的,解决win10下提示拒绝的错误 2022/05/10
' 优化函数:GetMatchHwndFromWindow 设置搜集窗口信息最多尝试10次
' 增加函数:FileToClipboard 设置指定文件到剪切板 2022/06/26
' 增加函数:Paste 粘贴内容,可以是字符串或文件
' 修正函数:GetWindowByClassNameEx 修复了do里面取类名的错误 2022/08/26
' 增加函数:GetWindowClassNameByHwnd 同GetClassNameByHwnd
'==============================================================================================
Option Explicit
'常量定义
Private Const SW_HIDE = 0
Private Const SW_SHOWNORMAL = 1
Private Const SW_NORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOWNOACTIVATE = 4
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_SHOWMINNOACTIVE = 7
Private Const SW_SHOWNA = 8
Private Const SW_RESTORE = 9
Private Const SW_SHOWDEFAULT = 10
Private Const SW_MAX = 10
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SM_CXSCREEN As Long = 0&
Private Const SM_CYSCREEN As Long = 1&
Private Const BM_CLICK = &HF5
Private Const GWL_ID = (-12)
Private Const GWL_STYLE = (-16)
Private Const WM_SYSCOMMAND = &H112
Private Const WM_SETFOCUS = &H7
Private Const WM_LBUTTONDOWN = &H201
Private Const WM_LBUTTONUP = &H202
Private Const WM_CLOSE = &H10
Private Const WM_SETTEXT = &HC
Private Const WM_GETTEXT = &HD
Private Const WM_GETTEXTLENGTH = &HE
Private Const SC_MINIMIZE = &HF020&
Private Const SC_MAXIMIZE = &HF030&
Private Const SC_RESTORE = &HF120&
Private Const SC_MOVE = &HF010&
Private Const MF_BYCOMMAND = &H0&
Private Const MF_ENABLED = &H0&
Private Const MF_GRAYED = &H1&
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
Private Const GWL_EXSTYLE = -20
Private Const WS_EX_TOPMOST = &H8
Private Const WS_VISIBLE = &H10000000
Private Const LWA_ALPHA = &H2
Private Const LWA_COLORKEY = &H1
Private Const WS_EX_LAYERED = &H80000
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Const CB_SETCURSEL = &H14E
Private Const PROCESS_ALL_ACCESS As Long = &H1F0FFF
Private Const WAIT_ABANDONED& = &H80&
Private Const WAIT_ABANDONED_0& = &H80&
Private Const WAIT_FAILED& = -1&
Private Const WAIT_IO_COMPLETION& = &HC0&
Private Const WAIT_OBJECT_0& = 0
Private Const WAIT_OBJECT_1& = 1
Private Const WAIT_TIMEOUT& = &H102&
Private Const INFINITE = &HFFFF
Private Const QS_HOTKEY& = &H80
Private Const QS_KEY& = &H1
Private Const QS_MOUSEBUTTON& = &H4
Private Const QS_MOUSEMOVE& = &H2
Private Const QS_PAINT& = &H20
Private Const QS_POSTMESSAGE& = &H8
Private Const QS_SENDMESSAGE& = &H40
Private Const QS_TIMER& = &H10
Private Const ERROR_ALREADY_EXISTS = 183&
Private Const QS_MOUSE& = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT& = (QS_MOUSE Or QS_KEY)
Private Const QS_ALLEVENTS& = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER Or QS_PAINT Or QS_HOTKEY)
Private Const QS_ALLINPUT& = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)
Private Const UNITS = 4294967296#
Private Const MAX_LONG = -2147483648#
Private Const GMEM_MOVEABLE = &H2
Private Const GMEM_ZEROINIT = &H40
Private Const CF_HDROP = &HF
'结构体定义
Private Type rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type DROPFILES
pFiles As Long
pT As POINTAPI
fNC As Long
fWide As Long
End Type
Public Enum enumShift
Horizontal
Vertical
Both
End Enum
Public Enum enumPositionMode
ABSOLUTE
RELATIVE
End Enum
'过滤窗口是否可见
Public Enum enumWindowVisible
HiddenWindow
DisplayedWindow
AllWindow
End Enum
Public Enum enumWindowState
vbNormal = 0
vbMinimized = 1
vbMaximized = 2
End Enum
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () 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
Private Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function MoveWindow Lib "user32" (ByVal hWnd As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetModuleFileNameEx Lib "PSAPI.DLL" Alias "GetModuleFileNameExW" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As Any, ByVal nSize As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32.dll" (ByVal nIndex As Long) As Long
Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpfn As Long, ByVal lParam As Long) As Long
Private Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function ModifyMenu Lib "user32" Alias "ModifyMenuA" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpString As Any) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As rect) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsZoomed Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetLayeredWindowAttributes Lib "user32.dll" (ByVal hWnd As Long, ByRef crKey As Long, ByRef bAlpha As Byte, ByRef dwFlags As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Private Declare Function IsWindowEnabled Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function EnableWindow Lib "user32" (ByVal hWnd As Long, ByVal fEnable As Long) As Long
Private Declare Function UpdateWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SetFocusAPI Lib "user32" Alias "SetFocus" (ByVal hWnd As Long) As Long
Private Declare Function QueryFullProcessImageName Lib "Kernel32.dll" Alias "QueryFullProcessImageNameW" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As Any, ByVal nSize As Long) As Long
Private Declare Function GetProcessImageFileName Lib "PSAPI.DLL" Alias "GetProcessImageFileNameA" (ByVal hProcess As Long, ByVal lpImageName As String, nSize As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function IsWow64Process Lib "kernel32" (ByVal hProcess As Long, ByRef Wow64Process As Long) As Long
Private Declare Function QueryDosDevice Lib "kernel32" Alias "QueryDosDeviceA" (ByVal lpDeviceName As String, ByVal lpTargetPath As String, ByVal ucchMax As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Private Const DelayOneTime = 300
Private hWnd_ As Long '窗口句柄
Private wReturn As New clsWindow '作为返回对象以便连用
Private Const strVersion = "V2.2"
Public DebugMe As Boolean '表示是否启用调试模式
'自定义变量声明
Private strWindowInfo$ '保存所有窗口的信息,格式为 句柄 文本内容
Private strWindowKeyWord$ '要参与的过滤的窗口的关键字,如果不需要过滤就留空
'移动和设置窗口到指定位置,以及设置窗口的宽高。
Public Function Move(Optional ByVal X, Optional ByVal Y, Optional ByVal nWidth, Optional ByVal nHeight) As Long
If IsMissing(X) Then X = Left
If IsMissing(Y) Then Y = Top
If IsMissing(nWidth) Then nWidth = Width
If IsMissing(nHeight) Then nHeight = Height
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Move 被调用。参数为 x=" & X & vbTab & " y=" & Y & vbTab & " nWidth=" & nWidth & vbTab & " nHeight=" & nHeight, False
End If
Move = MoveWindow(hWnd_, X, Y, nWidth, nHeight, True)
End Function
'移动窗口到屏幕中心
Public Function MoveToCenter() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 MoveToCenter 被调用。", False
End If
Dim X&, Y&, nWidth&, nHeight&
nWidth = Width
nHeight = Height
X = (GetSystemMetrics(SM_CXSCREEN) - nWidth) \ 2
Y = (GetSystemMetrics(SM_CYSCREEN) - nHeight) \ 2
MoveToCenter = MoveWindow(hWnd_, X, Y, nWidth, nHeight, True)
End Function
'得到当前窗口的句柄
Public Property Get hWnd() As Long
hWnd = hWnd_
End Property
'设置类的句柄
Public Property Let hWnd(ByVal hWnd As Long)
If IsWindow(hWnd) Then
hWnd_ = hWnd
Else
hWnd_ = 0
End If
End Property
'得到窗口的Left位置
Public Property Get Left() As Long
Dim rect As rect
If hWnd_ <> 0 Then
GetWindowRect hWnd_, rect
Left = rect.Left
Else
Left = -1
End If
End Property
'设置窗口的Left位置
Public Property Let Left(ByVal lngLeft As Long)
Move lngLeft
End Property
'得到窗口的Top位置
Public Property Get Top() As Long
Dim rect As rect
If hWnd_ <> 0 Then
GetWindowRect hWnd_, rect
Top = rect.Top
Else
Top = -1
End If
End Property
'设置窗口的Top位置
Public Property Let Top(ByVal lngTop As Long)
Move , lngTop
End Property
'得到窗口的Width大小
Public Property Get Width() As Long
Dim rect As rect
If hWnd_ <> 0 Then
GetWindowRect hWnd_, rect
Width = rect.Right - rect.Left
Else
Width = -1
End If
End Property
'设置窗口的Width
Public Property Let Width(ByVal lngWidth As Long)
Move , , lngWidth
End Property
'得到窗口的Height大小
Public Property Get Height() As Long
Dim rect As rect
If hWnd_ <> 0 Then
GetWindowRect hWnd_, rect
Height = rect.Bottom - rect.Top
Else
Height = -1
End If
End Property
'设置窗口的Width
Public Property Let Height(ByVal lngHeight As Long)
Move , , , lngHeight
End Property
'设置当前窗口的标题栏文字
Public Property Let Caption(ByVal strNewText As String)
If hWnd_ <> 0 Then
SendMessage hWnd_, WM_SETTEXT, 0&, ByVal strNewText
End If
End Property
'得到当前窗口的标题栏文字 使用GetWindowTextByHwnd函数
Public Property Get Caption() As String
Caption = GetText(hWnd_)
End Property
'根据句柄获得窗口内容
Public Function GetCaptionByHwnd(ByVal hWnd As Long) As String
GetCaptionByHwnd = GetTextByHwnd(hWnd)
End Function
'根据句柄获得窗口内容
Public Function GetTextByHwnd(ByVal hWnd As Long) As String
GetTextByHwnd = GetText(hWnd)
End Function
'根据句柄获得窗口类名
Public Function GetWindowClassNameByHwnd(ByVal hWnd As Long) As String
GetWindowClassNameByHwnd = GetClassNameByHwnd(hWnd)
End Function
'根据句柄获得窗口类名
Public Function GetClassNameByHwnd(ByVal hWnd As Long) As String
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetClassNameByHwnd 被调用。参数为 hWnd=" & hWnd, False
End If
Dim lngHwnd&, strClassNameCurrent As String * 255
GetClassName hWnd, strClassNameCurrent, 255 '获得窗口类
GetClassNameByHwnd = Replace(strClassNameCurrent, Chr$(0), "")
End Function
'根据句柄获得窗口内容 窗口专用
Public Function GetWindowTextByHwnd(ByVal hWnd As Long) As String
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowTextByHwnd 被调用。参数为 hWnd=" & hWnd, False
End If
Dim l&, strCaption$
l = GetWindowTextLength(hWnd)
strCaption = Space(l)
GetWindowText hWnd, strCaption, l + 1
GetWindowTextByHwnd = strCaption
End Function
'得到当前窗口的类名
Public Property Get ClassName() As String
ClassName = GetClassNameByHwnd(hWnd_)
End Property
'设置当前窗口的标题栏文字
Public Property Let Text(ByVal strNewText As String)
Caption = strNewText
End Property
'得到当前窗口的标题栏文字
Public Property Get Text() As String
Text = Caption
End Property
'设置当前窗口的标题栏文字
Public Property Let Value(ByVal strNewText As String)
Caption = strNewText
End Property
'得到当前窗口的标题栏文字
Public Property Get Value() As String
Value = Caption
End Property
'设置当前窗口的标题栏文字
Public Property Let Title(ByVal strNewText As String)
Caption = strNewText
End Property
'得到当前窗口的标题栏文字
Public Property Get Title() As String
Title = Caption
End Property
'窗口置前
Public Function SetPosFront() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 SetPosFront 被调用。", False
End If
If hWnd_ <> 0 Then
SetPosFront = SetWindowPos(hWnd_, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE)
End If
End Function
'窗口置后
Public Function SetPosBottom() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 SetPosBottom 被调用。", False
End If
If hWnd_ <> 0 Then
SetPosBottom = SetWindowPos(hWnd_, SW_SHOWNORMAL, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE)
End If
End Function
'设置窗口位置正常
Public Function SetPosNormal() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 SetPosNormal 被调用。", False
End If
If hWnd_ <> 0 Then
SetPosNormal = SetWindowPos(hWnd_, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE)
End If
End Function
'设置窗口最小化
Public Function Minimized() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Minimized 被调用。", False
End If
If hWnd_ <> 0 Then
' Minimized = ShowWindow(hWnd_, SW_SHOWMINIMIZED)
Minimized = SendMessage(hWnd_, WM_SYSCOMMAND, SC_MINIMIZE, ByVal 0&)
End If
End Function
'设置窗口最大化
Public Function Maximized() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Maximized 被调用。", False
End If
If hWnd_ <> 0 Then
' Maximized = ShowWindow(hWnd_, SW_SHOWMAXIMIZED)
Maximized = SendMessage(hWnd_, WM_SYSCOMMAND, SC_MAXIMIZE, ByVal 0&)
End If
End Function
'设置窗口还原
Public Function Restore() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Restore 被调用。", False
End If
If hWnd_ <> 0 Then
Restore = SendMessage(hWnd_, WM_SYSCOMMAND, SC_RESTORE, ByVal 0&)
End If
End Function
'设置窗口位置隐藏
Public Function Hide() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Hide 被调用。", False
End If
If hWnd_ <> 0 Then
Hide = ShowWindow(hWnd_, SW_HIDE)
End If
End Function
'设置窗口显示
Public Function Show(Optional ByVal ShowStatus As VbAppWinStyle = vbNormalFocus) As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Show 被调用。", False
End If
If hWnd_ <> 0 Then
Show = ShowWindow(hWnd_, ShowStatus)
End If
End Function
'设置窗口正常显示
Public Function Normal() As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Normal 被调用。", False
End If
If hWnd_ <> 0 Then
Normal = ShowWindow(hWnd_, SW_NORMAL)
End If
End Function
'设置当前窗口为活动窗口
Public Sub Focus()
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Focus 被调用。", False
End If
Dim hForeWnd&, dwForeID&, dwCurID&, isTopmostCurrentWindow As Boolean
isTopmostCurrentWindow = IsTopMost()
hForeWnd = GetForegroundWindow()
dwForeID = GetWindowThreadProcessId(hForeWnd, 0)
dwCurID = GetCurrentThreadId()
AttachThreadInput dwCurID, dwForeID, True
' ShowWindow hWnd_, SW_SHOWNORMAL
If Not isTopmostCurrentWindow Then '非置顶窗口可以设置置顶再取消置顶
SetWindowPos hWnd_, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE
SetWindowPos hWnd_, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE
End If
SetForegroundWindow hWnd_
AttachThreadInput dwCurID, dwForeID, False
End Sub
'设置当前窗口为活动窗口,有时无效,暂且保留
Public Sub Focus2()
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 Focus2 被调用。", False
End If
BringWindowToTop hWnd_
SetFocusAPI hWnd_
End Sub
'支持和VB中通常名称的方法
Public Sub SetFocus()
Call Focus
End Sub
'得到进程标识
Public Function ProcessID(Optional ByVal hWnd1&) As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 ProcessID 被调用。参数为 hWnd1=" & hWnd1, False
End If
If hWnd1 = 0 Then hWnd1 = hWnd_ '如果没写则表示处理当前存储的句柄
GetWindowThreadProcessId hWnd1, ProcessID
End Function
'得到进程标识
Public Function pid(Optional ByVal hWnd1&) As Long
pid = ProcessID(hWnd1)
End Function
'判断64位系统
Public Function IsWin64() As Boolean
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 IsWin64 被调用。", False
End If
Dim lngReturn As Long
Call IsWow64Process(GetCurrentProcess, lngReturn)
If lngReturn = 0 Then
IsWin64 = False '非64位系统
Else
IsWin64 = True '64位系统
End If
End Function
'根据窗口句柄得到应用程序的路径
Public Function AppPath(Optional ByVal hWn1&) As String
If hWn1 = 0 Then hWn1 = hWnd_ '如果没写则表示处理当前存储的句柄
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 ProcessID 被调用。参数为 hWn1=" & hWn1, False
End If
Dim dwProcessId As Long, hProcess As Long, i&, sTmp2 As String * 255, sTmp$
dwProcessId = ProcessID(hWn1)
If dwProcessId = 0 Then Exit Function
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessId)
AppPath = String$(255, vbNullChar)
If IsWin64 Then '64位系统
Call GetProcessImageFileName(hProcess, AppPath, 255)
For i = 65 To 90 'Asc("A") To Asc("Z")
Call QueryDosDevice(Chr$(i) & ":", sTmp2, 255)
sTmp = Split(sTmp2, vbNullChar, 2)(0) & "\"
If Len(sTmp) > 1 And InStr(1, AppPath, sTmp, vbTextCompare) = 1 Then
AppPath = Chr$(i) & ":" & Mid$(AppPath, Len(sTmp))
Exit For
End If
Next
Else
Call GetModuleFileNameEx(hProcess, 0, StrPtr(AppPath), 255)
End If
AppPath = Split(AppPath, vbNullChar, 2)(0)
Call CloseHandle(hProcess)
End Function
'得到exe名字,如果缺省就获取自己的exe名称
Public Function AppName(Optional ByVal hWnd1&) As String
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 AppName 被调用。参数为 hWnd1=" & hWnd1, False
End If
Dim v
If hWnd1 = 0 Then hWnd1 = hWnd_ '如果没写则表示处理当前存储的句柄
v = Split(AppPath(hWnd1) & "\", "\")
AppName = v(UBound(v) - 1)
End Function
'得到exe文件的启动参数
Public Function AppCommandLine(Optional ByVal lPid&) As String
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 AppCommandLine 被调用。参数为 lPid=" & lPid, False
End If
Dim objWMIService As Object
Dim colProcessList As Object
Dim objProcess As Object
Dim objProType As Object
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
If lPid = 0 Then lPid = Me.pid '如果没写则表示处理当前存储的PID
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where ProcessId='" & lPid & "'")
For Each objProcess In colProcessList
For Each objProType In objProcess.Properties_
If objProType.Name = "CommandLine" Then
AppCommandLine = objProType.Value & "" 'Value为Null时须连接空字符串才不会出错
Exit For
End If
Next
Next
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 AppCommandLine 执行完毕。", False
End If
End Function
'得到exe文件的启动参数
Public Function CommandLine(Optional ByVal lPid&) As String
If lPid = 0 Then lPid = Me.pid '如果没写则表示处理当前存储的PID
CommandLine = AppCommandLine(lPid&)
End Function
'检查窗口可见是否符合指定要求
Private Function CheckWindowVisible(ByVal lngHwnd&, ByVal windowVisible As enumWindowVisible) As Boolean
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 CheckWindowVisible 被调用。参数为 lngHwnd=" & lngHwnd & vbTab & " windowVisible=" & windowVisible, False
End If
If lngHwnd <> 0 Then
If windowVisible = AllWindow Then
CheckWindowVisible = True
ElseIf windowVisible = DisplayedWindow Then
If myIsWindowVisibled(lngHwnd) Then CheckWindowVisible = True
ElseIf windowVisible = HiddenWindow Then
If Not myIsWindowVisibled(lngHwnd) Then CheckWindowVisible = True
End If
End If
End Function
'设置根据窗体标题得到窗口句柄,可以指定等待几秒检测指定标题的窗体是否出现,默认是等待10秒
Public Function GetWindowByTitle(ByVal strTitle$, Optional ByVal intWaitSeconds& = 10, Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByTitle 被调用。参数为 strTitle=" & strTitle & vbTab & " intWaitSeconds=" & intWaitSeconds & vbTab & " windowVisible=" & windowVisible, False
End If
Dim lngDelayCount&
hWnd_ = 0
Do
hWnd_ = FindWindow(vbNullString, strTitle)
If CheckWindowVisible(hWnd_, windowVisible) Then Exit Do '窗口可见度检查,符合条件表示找到了即退出
lngDelayCount = lngDelayCount + DelayOneTime
If lngDelayCount >= intWaitSeconds * 1000 Then Exit Do '未发现关键字为 strTitle
Wait DelayOneTime
hWnd_ = 0
Loop
wReturn.hWnd = hWnd_
Set GetWindowByTitle = wReturn
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByTitle 执行完毕。", False
End If
End Function
'和GetWindowByTitle函数功能类似,只是这个是模糊匹配
Public Function GetWindowByTitleEx(ByVal strTitle$, Optional ByVal intWaitSeconds& = 10, Optional ByRef hWndAll, Optional isUseRegExp As Boolean = False, Optional ByVal checkPid = "", Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByTitleEx 被调用。参数为 strTitle=" & strTitle & vbTab & " intWaitSeconds=" & intWaitSeconds & vbTab & " isUseRegExp=" & isUseRegExp & vbTab & " checkPid=" & checkPid & vbTab & " windowVisible=" & windowVisible, False
End If
Dim lngHwnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strHwndAllTmp$
Dim isMatch As Boolean
Do
hWnd_ = 0
Do
lngHwnd = FindWindowEx(0, lngHwnd, vbNullString, vbNullString)
If CheckWindowVisible(lngHwnd, windowVisible) Then '窗口可见度检查,符合条件则继续下面的检查
strCaption = GetWindowTextByHwnd(lngHwnd) '使用窗口专用的函数
If strCaption <> "" Then
If Not isUseRegExp Then
isMatch = InStr(strCaption, strTitle) > 0
Else
isMatch = regTest(strCaption, strTitle)
End If
Else
isMatch = (strTitle = "") '用户可能就需要获取窗口标题为空的呢
End If
If isMatch Then
strHwndAllTmp = strHwndAllTmp & CStr(lngHwnd) & " "
If checkPid <> "" Then '如果pid相等,优先放到第一个
If checkPid = ProcessID() Then strHwndAllTmp = lngHwnd & " " & strHwndAllTmp
End If
End If
End If
Loop Until lngHwnd = 0 '这层loop是遍历所有窗口的
If strHwndAllTmp <> "" Then Exit Do '如果一遍循环得到结果了就退出
Wait DelayOneTime
lngDelayCount = lngDelayCount + DelayOneTime '计算累计延时了多少,然后和用户设置的延时时间对比
If lngDelayCount >= intWaitSeconds * 1000 Then Exit Do '超时未发现关键字strTitle
Loop
strHwndAllTmp = Trim$(strHwndAllTmp)
If Not IsMissing(hWndAll) Then hWndAll = strHwndAllTmp '如果需要搜集所有句柄,那么则使用这个参数返回
If strHwndAllTmp <> "" Then
wReturn.hWnd = Split(strHwndAllTmp, " ")(0)
Else
wReturn.hWnd = 0
End If
Set GetWindowByTitleEx = wReturn
hWnd_ = wReturn.hWnd
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByTitleEx 执行完毕。", False
End If
End Function
'设置根据窗体类名返回窗体句柄,可以指定等待几秒检测指定标题的窗体是否出现,默认是等待10秒
Public Function GetWindowByClassName(ByVal strClassName$, Optional ByVal intWaitSeconds& = 10, Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByClassName 被调用。参数为 strClassName=" & strClassName & vbTab & " intWaitSeconds=" & intWaitSeconds & vbTab & " windowVisible=" & windowVisible, False
End If
Dim lngDelayCount&
hWnd_ = 0
Do
hWnd_ = FindWindow(strClassName, vbNullString)
If CheckWindowVisible(hWnd_, windowVisible) Then Exit Do '窗口可见度检查,符合条件表示找到了即退出
lngDelayCount = lngDelayCount + DelayOneTime
If lngDelayCount >= intWaitSeconds * 1000 Then Exit Do '超时未发现关键字strClassName
Wait DelayOneTime
hWnd_ = 0
Loop
wReturn.hWnd = hWnd_
Set GetWindowByClassName = wReturn
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByClassName 执行完毕。", False
End If
End Function
'设置根据窗体类名得到窗口句柄,可以指定等待几秒检测指定类名的窗体是否出现,默认是等待10秒,允许使用正则表达式
Public Function GetWindowByClassNameEx(ByVal strClassName$, Optional ByVal intWaitSeconds& = 10, Optional ByRef hWndAll, Optional isUseRegExp = False, Optional ByVal checkPid = "", Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByClassNameEx 被调用。参数为 strClassName=" & strClassName & vbTab & " intWaitSeconds=" & intWaitSeconds & vbTab & " isUseRegExp=" & isUseRegExp & vbTab & " checkPid=" & checkPid & vbTab & " windowVisible=" & windowVisible, False
End If
Dim lngHwnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strClassNameCurrent As String
Dim strHwndAllTmp$
Dim isMatch As Boolean
hWnd_ = 0
Do
Do
lngHwnd = FindWindowEx(0, lngHwnd, vbNullString, vbNullString)
If CheckWindowVisible(lngHwnd, windowVisible) Then
strClassNameCurrent = GetWindowClassNameByHwnd(lngHwnd)
If strClassNameCurrent <> "" Then
If Not isUseRegExp Then
isMatch = InStr(strClassNameCurrent, strClassName) > 0
Else
isMatch = regTest(strClassNameCurrent, strClassName)
End If
Else
isMatch = (strClassName = "") '用户可能就需要获取窗口标题为空的呢
End If
If isMatch Then
strHwndAllTmp = strHwndAllTmp & CStr(lngHwnd) & " "
If checkPid <> "" Then '如果pid相等,优先放到第一个
If checkPid = ProcessID() Then strHwndAllTmp = lngHwnd & " " & strHwndAllTmp
End If
End If
End If
Loop Until lngHwnd = 0
If strHwndAllTmp <> "" Then Exit Do
Wait DelayOneTime
lngDelayCount = lngDelayCount + DelayOneTime '计算累计延时了多少,然后和用户设置的延时时间对比
If lngDelayCount >= intWaitSeconds * 1000 Then Exit Do '超时未发现关键字strTitle
Loop
strHwndAllTmp = Trim$(strHwndAllTmp)
If Not IsMissing(hWndAll) Then hWndAll = strHwndAllTmp '如果需要搜集所有句柄,那么则使用这个参数返回
If strHwndAllTmp <> "" Then
wReturn.hWnd = Split(strHwndAllTmp, " ")(0)
Else
wReturn.hWnd = 0
End If
Set GetWindowByClassNameEx = wReturn
hWnd_ = wReturn.hWnd
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByClassNameEx 执行完毕。", False
End If
End Function
'设置根据进程名称返回窗体句柄,可以指定等待几秒检测指定进程名称的窗体是否出现,默认是等待10秒
Public Function GetWindowByAppName(ByVal strAppName$, Optional ByVal intWaitSeconds& = 10, Optional ByVal isWholeMatch As Boolean = False, Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByAppName 被调用。参数为 strAppName=" & strAppName & vbTab & " intWaitSeconds=" & intWaitSeconds & vbTab & " isWholeMatch=" & isWholeMatch & vbTab & " windowVisible=" & windowVisible, False
End If
Dim lngHwnd&, currWnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strWindowClass As String * 255
Dim isMatch As Boolean
Dim strGetHwndAppName$
Dim strHwnd
strAppName = LCase$(strAppName)
hWnd_ = 0
Do
currWnd = GetDesktopWindow()
currWnd = GetWindow(currWnd, GW_CHILD)
Do While currWnd <> 0
If CheckWindowVisible(currWnd, windowVisible) Then
strGetHwndAppName = LCase$(AppName(currWnd)) '根据句柄得到进程名字,都转换成小写
If isWholeMatch Then '判断是否完全相等
isMatch = (LCase$(strGetHwndAppName) = strAppName)
Else
isMatch = (InStr(LCase$(strGetHwndAppName), strAppName) > 0)
End If
strCaption = GetWindowTextByHwnd(currWnd)
If InStr("|MSCTFIME UI|Program Manager|M|Default IME|", strCaption) = 0 And isMatch Then '过滤掉输入法相关的窗口
hWnd_ = currWnd
Exit Do
End If
End If
currWnd = GetWindow(currWnd, GW_HWNDNEXT)
DoEvents
Loop
If hWnd_ <> 0 Then Exit Do
Wait DelayOneTime
lngDelayCount = lngDelayCount + DelayOneTime '计算累计延时了多少,然后和用户设置的延时时间对比
If lngDelayCount >= intWaitSeconds * 1000 Then Exit Do '超时未发现关键字strTitle
Loop
wReturn.hWnd = hWnd_
Set GetWindowByAppName = wReturn
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByAppName 执行完毕。", False
End If
End Function
'设置根据进程名称返回窗体句柄,可以指定等待几秒检测指定进程名称的窗体是否出现,默认是等待10秒,原理还是枚举所有窗体检查它们的所属进程,只不过是根据目标的进程名跟strAppName比对
Public Function GetWindowByAppNameEx(ByVal strAppName$, Optional ByVal intWaitSeconds& = 10, Optional ByRef hWndAll, Optional isUseRegExp = False, Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数 GetWindowByAppNameEx 被调用。参数为 strAppName=" & strAppName & vbTab & " intWaitSeconds=" & intWaitSeconds & vbTab & " isUseRegExp=" & isUseRegExp & vbTab & " windowVisible=" & windowVisible, False
End If
Dim lngHwnd&, currWnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strWindowClass As String * 255
Dim strHwndAllTmp$
Dim isMatch As Boolean
Dim strAppNameCurrent$
Dim strHwnd
strAppName = LCase$(strAppName)
hWnd_ = 0
strHwndAllTmp = ""
Do
currWnd = GetDesktopWindow()
currWnd = GetWindow(currWnd, GW_CHILD)
Do While currWnd <> 0
If CheckWindowVisible(currWnd, windowVisible) Then '窗口可见度检查,符合条件则继续下面的检查
strAppNameCurrent = LCase$(AppName(currWnd)) '根据句柄得到进程名字,都转换成小写
If strAppNameCurrent <> "" Then
If Not isUseRegExp Then '如果不用正则就包含关系匹配
isMatch = InStr(strAppNameCurrent, strAppName) > 0
Else
isMatch = regTest(strAppNameCurrent, strAppName)
End If
Else
isMatch = (strAppName = "") '用户可能就需要获取窗口标题为空的呢
End If
strCaption = GetWindowTextByHwnd(currWnd)
If InStr("|MSCTFIME UI|Program Manager|M|Default IME|", strCaption) = 0 And isMatch Then '过滤掉输入法相关的窗口
strHwndAllTmp = strHwndAllTmp & CStr(currWnd) & " "
End If
End If
currWnd = GetWindow(currWnd, GW_HWNDNEXT)
DoEvents
Loop
If strHwndAllTmp <> "" Then Exit Do '表示匹配到了一部分数据
Wait DelayOneTime
lngDelayCount = lngDelayCount + DelayOneTime '计算累计延时了多少,然后和用户设置的延时时间对比
If lngDelayCount >= intWaitSeconds * 1000 Then Exit Do '超时未发现关键字strTitle
Loop
strHwndAllTmp = Trim$(strHwndAllTmp)
If Not IsMissing(hWndAll) Then hWndAll = strHwndAllTmp '如果需要搜集所有句柄,那么则使用这个参数返回
If strHwndAllTmp <> "" Then
wReturn.hWnd = Split(strHwndAllTmp, " ")(0)
Else
wReturn.hWnd = 0
End If
Set GetWindowByAppNameEx = wReturn
hWnd_ = wReturn.hWnd