-
Notifications
You must be signed in to change notification settings - Fork 3
/
clsWindow.cls
1534 lines (1450 loc) · 65.8 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.2
'描 述:一个操作windows窗口的类,可对窗口进行很多常用的操作(类名为clsWindow)
'使用范例:Dim window As New clsWindow
' window.GetWindowByTitle("计算器").closeWindow
'编 程:sysdzw 原创开发,如果有需要对模块扩充或更新的话请邮箱发我一份,共同维护
'发布日期:2013/06/01
'博 客:http://blog.csdn.net/sysdzw
'用户手册:https://www.kancloud.cn/sysdzw/clswindow/
'Email :sysdzw@163.com
'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(wwbing@gmail.com)提供了一些函数和方法属性:
' 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 根据网友小凡的bug反馈(句柄和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
'==============================================================================================
Option Explicit
'常量定义
Private Const SW_MINIMIZE = 6
Private Const SW_SHOW = 5
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWMINIMIZED = 2
Private Const SW_HIDE = 0
Private Const SW_NORMAL = 1
Private Const SW_SHOWNORMAL = 1
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_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_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 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
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 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 "Kernel32.dll" Alias "GetProcessImageFileNameW" (ByVal hProcess As Long, ByVal lpFilename As Any, ByVal 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 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 = 500
Private hWnd_ As Long '窗口句柄
Private wReturn As New clsWindow '作为返回对象以便连用
Private Const strVersion = "V2.2"
Public DebugMe As Boolean '表示是否启用调试模式
'自定义变量声明
Private strWindowInfo$ '保存所有窗口的信息,格式为 句柄 文本内容
Private strWindowKeyWord$ '要参与的过滤的窗口的关键字,如果不需要过滤就留空
Private strTmp$, isWholeEx As Boolean
'移动和设置窗口到指定位置,以及设置窗口的宽高。
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
Move = MoveWindow(hWnd_, x, y, nWidth, nHeight, True)
End Function
'移动窗口到屏幕中心
Public Function MoveToCenter() As Long
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
'得到当前窗口的标题栏文字
Public Property Get Caption() As String
Caption = GetTextByHwnd(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
' '方案1 性能欠佳
' Dim Txt2() As Byte, i&
' i = SendMessage(hWnd, WM_GETTEXTLENGTH, 0&, 0&)
' If i = 0 Then Exit Function '没有内容
' ReDim Txt2(i)
' SendMessage hWnd, WM_GETTEXT, i + 1, Txt2(0)
' ReDim Preserve Txt2(i - 1)
' GetTextByHwnd = StrConv(Txt2, vbUnicode)
'
'方案2 混合方案,尽量减少api调用(本代码由网友小凡提供)
Dim Txt2() As Byte, i&
Const lMaxLength& = 6
ReDim Txt2(lMaxLength&)
SendMessage Hwnd, WM_GETTEXT, lMaxLength&, Txt2(0)
If Txt2(0) = 0 Then Exit Function '没有内容
For i = 1 To lMaxLength&
If Txt2(i) = 0 Then Exit For '结束
Next
If i >= lMaxLength& - 1 Then '如果已取内容不完整
i = SendMessage(Hwnd, WM_GETTEXTLENGTH, 0&, 0&)
If i = 0 Then Exit Function '没有内容
ReDim Txt2(i) '须比实际内容多设一个字节来装结束符0
SendMessage Hwnd, WM_GETTEXT, i + 1, Txt2(0)
End If
ReDim Preserve Txt2(i - 1) '去掉多的字节
GetTextByHwnd = StrConv(Txt2, vbUnicode) '转ASI字串为宽字串
End Function
'设置当前窗口的标题栏文字
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 hWnd_ <> 0 Then
SetPosFront = SetWindowPos(hWnd_, -1, 0, 0, 0, 0, 3)
End If
End Function
'窗口置后
Public Function SetPosBottom() As Long
If hWnd_ <> 0 Then
SetPosBottom = SetWindowPos(hWnd_, 1, 0, 0, 0, 0, 3)
End If
End Function
'设置窗口位置正常
Public Function SetPosNormal() As Long
If hWnd_ <> 0 Then
SetPosNormal = SetWindowPos(hWnd_, -2, 0, 0, 0, 0, 3)
End If
End Function
'设置窗口最小化
Public Function Minimized() As Long
If hWnd_ <> 0 Then
Minimized = ShowWindow(hWnd_, SW_SHOWMINIMIZED)
End If
End Function
'设置窗口最大化
Public Function Maximized() As Long
If hWnd_ <> 0 Then
Maximized = ShowWindow(hWnd_, SW_SHOWMAXIMIZED)
End If
End Function
'设置窗口位置隐藏
Public Function Hide() As Long
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 hWnd_ <> 0 Then
Show = ShowWindow(hWnd_, ShowStatus)
End If
End Function
'设置窗口正常显示
Public Function Normal() As Long
If hWnd_ <> 0 Then
Normal = ShowWindow(hWnd_, SW_NORMAL)
End If
End Function
'设置当前窗口为活动窗口
Public Sub Focus()
Dim Hwnd&, hForeWnd&, dwForeID&, dwCurID&
Hwnd = hWnd_
hForeWnd = GetForegroundWindow()
dwForeID = GetWindowThreadProcessId(hForeWnd, 0)
dwCurID = GetCurrentThreadId()
AttachThreadInput dwCurID, dwForeID, True
' ShowWindow hWnd, SW_SHOWNORMAL
SetWindowPos Hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE
SetWindowPos Hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE + SWP_NOMOVE
SetForegroundWindow Hwnd
AttachThreadInput dwCurID, dwForeID, False
End Sub
'支持和VB中通常名称的方法
Public Sub SetFocus()
Call Focus
End Sub
'得到进程标识
Public Function ProcessID(Optional ByVal my_hWnd) As Long
Dim l&
If IsMissing(my_hWnd) Then
If hWnd_ <> 0 Then GetWindowThreadProcessId hWnd_, l
Else
GetWindowThreadProcessId my_hWnd, l
End If
ProcessID = l
End Function
'得到进程标识
Public Function pid(Optional ByVal my_hWnd) As Long
If IsMissing(my_hWnd) Then
pid = ProcessID
Else
pid = ProcessID(my_hWnd)
End If
End Function
'根据窗口句柄得到应用程序的路径
Public Function AppPath(Optional ByVal hwn1) As String
Dim dwProcessID As Long, hProcess As Long, hModule As Long
Dim nSize As Long
If IsMissing(hwn1) Then '如果没写则表示处理当前存储的句柄
If hWnd_ <> 0 Then dwProcessID = ProcessID() 'GetWindowThreadProcessId hWnd_, dwProcessID
Else
dwProcessID = ProcessID(hwn1)
End If
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, dwProcessID)
AppPath = Space$(255)
nSize = GetModuleFileNameEx(hProcess, 0, StrPtr(AppPath), 255) 'win7x64下获取为空,暂无解决方案 2020-2-5
AppPath = Mid$(AppPath, 1, nSize)
Call CloseHandle(hProcess)
End Function
'得到exe名字,如果缺省就获取自己的exe名称
Public Function AppName(Optional ByVal hwnd1) As String
Dim v
Static i%
If IsMissing(hwnd1) Then
If hWnd_ <> 0 Then v = Split(AppPath, "\")
Else
v = Split(AppPath(hwnd1), "\")
End If
i = i + 1
If UBound(v) >= 0 Then AppName = v(UBound(v))
End Function
'得到exe文件的启动参数
Public Function AppCommandLine(Optional ByVal strAppName) As String
If hWnd_ <> 0 Then
Dim objWMIService As Object
Dim colProcessList As Object
Dim objProcess As Object
Dim objProType As Object
Dim strResult As String
Set objWMIService = GetObject("winmgmts=" & "{impersonationlevel=impersonate}!//./root/cimv2")
If IsMissing(strAppName) Then
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name='" & AppName() & "'")
Else
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name='" & strAppName & "'")
End If
If colProcessList.Count <> 0 Then
For Each objProcess In colProcessList
For Each objProType In objProcess.Properties_
If objProType.Name = "CommandLine" Then
strResult = strResult & objProType.Value & vbCrLf
Exit For
End If
Next
Next
End If
If strResult <> "" Then
AppCommandLine = strResult
End If
End If
End Function
'得到exe文件的启动参数
Public Function CommandLine(Optional ByVal strAppName) As String
If IsMissing(strAppName) Then
If hWnd_ <> 0 Then CommandLine = AppCommandLine
Else
CommandLine = AppCommandLine(strAppName)
End If
End Function
'设置根据窗体标题得到窗口句柄,可以指定等待几秒检测指定标题的窗体是否出现,默认是等待10秒
Public Function GetWindowByTitle(ByVal strTitle$, Optional ByVal intWaitSeconds& = 10, Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
Dim lngDelayCount&
hWnd_ = 0
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetWindowByTitle被调用" & vbCrLf & "参数为 strTitle=" & strTitle & vbTab & " intWaitSeconds=" & intWaitSeconds, False
End If
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
End Function
'检查窗口可见是否符合指定要求
Private Function CheckWindowVisible(ByVal lngHwnd&, ByVal windowVisible As enumWindowVisible) As Boolean
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
'和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
Dim lngHwnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strHwndAllTmp$
Dim isMatch As Boolean
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetWindowByTitleEx被调用" & vbCrLf & "参数为 strTitle=" & strTitle & vbTab & " intWaitSeconds=" & intWaitSeconds, False
End If
Do
hWnd_ = 0
Do
lngHwnd = FindWindowEx(0, lngHwnd, vbNullString, vbNullString)
If CheckWindowVisible(lngHwnd, windowVisible) Then '窗口可见度检查,符合条件则继续下面的检查
strCaption = GetTextByHwnd(lngHwnd)
If Not isUseRegExp Then
isMatch = InStr(strCaption, strTitle) > 0
Else
isMatch = regTest(strCaption, 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
End Function
'设置根据窗体类名返回窗体句柄,可以指定等待几秒检测指定标题的窗体是否出现,默认是等待10秒
Public Function GetWindowByClassName(ByVal strClassName$, Optional ByVal intWaitSeconds& = 10, Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
Dim lngDelayCount&
hWnd_ = 0
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetWindowByClassName被调用" & vbCrLf & "参数为 strClassName=" & strClassName & vbTab & " intWaitSeconds=" & intWaitSeconds, False
End If
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
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
Dim lngHwnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strWindowClass As String * 255
Dim strHwndAllTmp$
Dim isMatch As Boolean
hWnd_ = 0
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetWindowByClassNameEx被调用" & vbCrLf & "参数为 strClassName=" & strClassName & vbTab & " intWaitSeconds=" & intWaitSeconds, False
End If
Do
Do
lngHwnd = FindWindowEx(0, lngHwnd, vbNullString, vbNullString)
If CheckWindowVisible(lngHwnd, windowVisible) Then
GetClassName lngHwnd, strWindowClass, 255 '获得窗口类
strWindowClass = Replace(strWindowClass, Chr$(0), "")
If Not isUseRegExp Then
isMatch = InStr(strWindowClass, strClassName) > 0
Else
isMatch = regTest(strWindowClass, 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
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
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)
writeToFile "DEBUG_exe.txt", ""
hWnd_ = 0
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetWindowByAppName被调用" & vbCrLf & "参数为 strAppName=" & strAppName & vbTab & " intWaitSeconds=" & intWaitSeconds, False
End If
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 = GetTextByHwnd(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
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
Dim lngHwnd&, currWnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strWindowClass As String * 255
Dim strHwndAllTmp$
Dim isMatch As Boolean
Dim strGetHwndAppName$
Dim strHwnd
strAppName = LCase$(strAppName)
hWnd_ = 0
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetWindowByAppNameEx被调用" & vbCrLf & "参数为 strAppName=" & strAppName & vbTab & " intWaitSeconds=" & intWaitSeconds, False
End If
strHwndAllTmp = ""
Do
currWnd = GetDesktopWindow()
currWnd = GetWindow(currWnd, GW_CHILD)
Do While currWnd <> 0
If CheckWindowVisible(currWnd, windowVisible) Then
strGetHwndAppName = LCase$(AppName(currWnd)) '根据句柄得到进程名字,都转换成小写
If Not isUseRegExp Then '如果不用正则就包含关系匹配
isMatch = (InStr(strGetHwndAppName, strAppName) > 0)
Else
isMatch = regTest(strGetHwndAppName, strAppName)
End If
strCaption = GetTextByHwnd(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
End Function
'根据pid获得窗体
Public Function GetWindowByPID(ByVal pid As Long, Optional ByVal intWaitSeconds& = 10, Optional ByRef hWndAll, Optional ByVal windowVisible As enumWindowVisible = AllWindow) As clsWindow
Dim lngHwnd&, currWnd&, l&, lngDelayCount&
Dim strCaption As String
Dim strWindowClass As String * 255
Dim strHwndAllTmp$
Dim isMatch As Boolean
Dim currentPID$
Dim strHwnd
hWnd_ = 0
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetWindowByPID被调用" & vbCrLf & "参数为 PID=" & pid & vbTab & " hWndAll=" & hWndAll, False
End If
strHwndAllTmp = ""
Dim aaa&, bbb&
Do '循环找几遍
currWnd = GetDesktopWindow()
currWnd = GetWindow(currWnd, GW_CHILD)
Do While currWnd <> 0
If CheckWindowVisible(currWnd, windowVisible) Then
currentPID = ProcessID(currWnd) '得到pid
isMatch = (currentPID = pid)
If isMatch Then
strCaption = GetTextByHwnd(currWnd)
If InStr("|MSCTFIME UI|Program Manager|M|Default IME|", strCaption) = 0 Then '过滤掉输入法相关的窗口
strHwndAllTmp = strHwndAllTmp & CStr(currWnd) & " "
End If
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
Exit Do
Loop
strHwndAllTmp = Trim$(strHwndAllTmp)
If Not IsMissing(hWndAll) Then hWndAll = strHwndAllTmp '如果需要搜集所有句柄,那么则使用这个参数返回
If strHwndAllTmp <> "" Then
Dim vHwnd
vHwnd = Split(strHwndAllTmp, " ")
wReturn.Hwnd = vHwnd(UBound(vHwnd))
Else
wReturn.Hwnd = 0
End If
Set GetWindowByPID = wReturn
hWnd_ = wReturn.Hwnd
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'以下几个方法为网友wwb(邮箱:wwbing@gmail.com)所增加
'日期:2013-6-26
'具体有:CheckWindow, Load, WindowState, Visible, hDC,
' AlphaBlend, Enabled, Refresh, TransparentColor, ZOrder
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'检查当前窗口句柄是否还有效
Private Function CheckWindow(Optional ByVal lngHwnd) As Long
If IsMissing(lngHwnd) Then lngHwnd = hWnd_
CheckWindow = IsWindow(lngHwnd)
End Function
'Load 窗口载入的入口
Public Function Load(WindowHwnd As Variant) As clsWindow
If IsNumeric(WindowHwnd) Then
hWnd_ = CLng(WindowHwnd)
If CheckWindow Then
wReturn.Hwnd = hWnd_
Set Load = wReturn
Else
MsgBox "类初始化错误!句柄“" & WindowHwnd & "”对应的窗口不存在!", vbExclamation
End If
Else
MsgBox "类初始化错误!设置的句柄“" & WindowHwnd & "”应当为一个数字。", vbExclamation
End If
End Function
'直接由句柄指定窗口,与直接设置w.hwnd或者load方法同等效果
Public Function GetWindowByHwnd(WindowHwnd As Variant) As clsWindow
If IsNumeric(WindowHwnd) Then
hWnd_ = CLng(WindowHwnd)
If CheckWindow Then
wReturn.Hwnd = hWnd_
Set GetWindowByHwnd = wReturn
Else
MsgBox "类初始化错误!句柄“" & WindowHwnd & "”对应的窗口不存在!", vbExclamation
End If
Else
MsgBox "类初始化错误!设置的句柄“" & WindowHwnd & "”应当为一个数字。", vbExclamation
End If
End Function
'直接获得当前鼠标下的窗口或控件的句柄
Public Function GetWindowByCursorPos() As clsWindow
Dim tPoint As POINTAPI
GetCursorPos tPoint '获得当前鼠标位置
hWnd_ = WindowFromPoint(tPoint.x, tPoint.y)
wReturn.Hwnd = hWnd_
Set GetWindowByCursorPos = wReturn
End Function
'根据指定的点获取窗口或控件的句柄
Public Function GetWindowByPoint(ByVal x As Long, ByVal y As Long) As clsWindow
hWnd_ = WindowFromPoint(x, y)
wReturn.Hwnd = hWnd_
Set GetWindowByPoint = wReturn
End Function
'根据控件的类名设置控件的显示文字
Public Function SetElementTextByClassName(ByVal strClassName$, ByVal strNewText$, Optional ByVal intIndex% = 1, Optional ByVal UseRegExp As Boolean = False) As Boolean
Dim lngControlsHwnd As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数SetElementTextByClassName被调用" & vbCrLf & "参数为 strClassName=" & strClassName & vbTab & " strNewText=" & strNewText & vbTab & " intIndex=" & intIndex & vbTab & " UseRegExp=" & UseRegExp, False
writeToFile "DEBUG.txt", Now & " 函数SetElementTextByClassName中调用函数getMatchHwndFromWindow", False
End If
'如果使用非正则模式需要替换特殊字符,因为函数内部始终用正则查询的,防止干扰
If Not UseRegExp Then
strClassName = replaceReg(strClassName, "([\\+-\.()\[\]{}?*\|])", "\$1")
lngControlsHwnd = GetMatchHwndFromWindow("^([-\d]+)\s+[-\d]+\s" & strClassName & "\s.*?$", intIndex)
Else
lngControlsHwnd = GetMatchHwndFromWindow(strClassName, intIndex, UseRegExp, True) '最后的true可以省略,默认为类名,否则就是文本
End If
If lngControlsHwnd <> 0 Then
SendMessage lngControlsHwnd, WM_SETTEXT, 0&, ByVal strNewText
SetElementTextByClassName = True
Else
SetElementTextByClassName = False
End If
End Function
'根据控件的类名追加显示控件的文字
Public Function AppendElementTextByClassName(ByVal strClassName$, ByVal strAppendText$, Optional ByVal intIndex% = 1, Optional ByVal UseRegExp As Boolean = False) As Long
Dim lngControlsHwnd As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数AppendElementTextByClassName被调用" & vbCrLf & "参数为 strClassName=" & strClassName & vbTab & " strAppendText=" & strAppendText & vbTab & " UseRegExp=" & UseRegExp, False
writeToFile "DEBUG.txt", Now & " 函数AppendElementTextByClassName中调用函数getMatchHwndFromWindow", False
End If
'如果使用非正则模式需要替换特殊字符,因为函数内部始终用正则查询的,防止干扰
If Not UseRegExp Then
strClassName = replaceReg(strClassName, "([\\+-\.()\[\]{}?*\|])", "\$1")
lngControlsHwnd = GetMatchHwndFromWindow("^([-\d]+)\s+[-\d]+\s" & strClassName & "\s.*?$", intIndex)
Else
lngControlsHwnd = GetMatchHwndFromWindow(strClassName, intIndex, UseRegExp, True) '最后的true可以省略,默认为类名,否则就是文本
End If
If lngControlsHwnd <> 0 Then
strAppendText = RTrim$(GetElementTextByClassName(strClassName, intIndex, UseRegExp)) & strAppendText
AppendElementTextByClassName = SendMessage(lngControlsHwnd, WM_SETTEXT, 0&, ByVal strAppendText)
Else
AppendElementTextByClassName = 0
End If
End Function
'根据控件的类名得到控件的显示文字
Public Function GetElementTextByClassName(ByVal strClassName$, Optional ByVal intIndex = 1, Optional ByVal UseRegExp As Boolean = False) As String
Dim lngControlsHwnd As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetElementTextByClassName被调用" & vbCrLf & "参数为 strClassName=" & strClassName & vbTab & " intIndex=" & intIndex, False
writeToFile "DEBUG.txt", Now & " 函数GetElementTextByClassName中调用函数getMatchHwndFromWindow", False
End If
'如果使用非正则模式需要替换特殊字符,因为函数内部始终用正则查询的,防止干扰
If Not UseRegExp Then
strClassName = replaceReg(strClassName, "([\\+-\.()\[\]{}?*\|])", "\$1")
lngControlsHwnd = GetMatchHwndFromWindow("^([-\d]+)\s+[-\d]+\s" & strClassName & "\s.*?$", intIndex)
Else
lngControlsHwnd = GetMatchHwndFromWindow(strClassName, intIndex, UseRegExp, True) '最后的true可以省略,默认为类名,否则就是文本
End If
If lngControlsHwnd <> 0 Then
GetElementTextByClassName = GetTextByHwnd(lngControlsHwnd)
Else
GetElementTextByClassName = ""
End If
End Function
'根据控件的内容得到控件的显示文字
Public Function GetElementTextByText(ByVal strText$, Optional ByVal intIndex = 1, Optional ByVal UseRegExp As Boolean = False) As String
Dim lngControlsHwnd As Long
If DebugMe Then
writeToFile "DEBUG.txt", Now & " 函数GetElementTextByText被调用" & vbCrLf & "参数为 strText=" & strText & vbTab & " intIndex=" & intIndex, False
writeToFile "DEBUG.txt", Now & " 函数GetElementTextByText中调用函数GetMatchHwndFromWindow", False
End If
'如果使用非正则模式需要替换特殊字符,因为函数内部始终用正则查询的,防止干扰
If Not UseRegExp Then