1,486
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Public Const GW_CHILD = 5
Public Const GW_HWNDNEXT = 2
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Integer) As Integer
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
'枚举某一个窗口的子窗口
Public Sub ListAllChildWindow(Optional bParentHwnd As Long)
On Error Resume Next
Dim Str As String * 256 '定义str用来存储子窗体类名
Dim hd As Long, w2 As String
If bParentHwnd = 0 Then bParentHwnd = GetDesktopWindow '桌面hwnd
hd = GetWindow(bParentHwnd, GW_CHILD) '获取Form的第一个子窗体句柄hd
Do While hd <> 0 '如果子窗体查找完毕hd返回0,所以用hd<>0作为继续查找条件
GetClassName hd, Str, 256 '获得子窗体类名存储在str里
w2 = Left$(Str, InStr(Str, Chr$(0)) - 1)
Debug.Print w2 & "----" & hd
hd = GetWindow(hd, GW_HWNDNEXT) '获取下一个同级子窗体句柄hd
Loop
End Sub
Sub Test()
ListAllChildWindow Me.Hwnd
End Sub
’下面是得到的结果:
'ThunderTextBox ----3996(正确的hwnd: 23728126)
'ThunderTextBox ----4094(正确的hwnd: 8195996)
'ThunderCommandButton ----3862