vb中 如何获得窗体中所有控件的句柄?
1个回答
展开全部
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () 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 Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Sub Command1_Click()
Call List1_Click
End Sub
Private Sub Command2_Click()
Dim hwnd As Long
Dim s As String, t As String
List1.Clear
hwnd = GetDesktopWindow()
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1.AddItem "桌面:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
hwnd = GetWindow(hwnd, GW_CHILD Or GW_HWNDFIRST)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1.AddItem "窗口:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
While hwnd <> 0
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1.AddItem "窗口:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
Wend
End Sub
Private Sub Form_Load()
Command1.Caption = "获取所有控件"
Command2.Caption = "遍历所有窗体"
End Sub
Private Sub EnumAllHandles(ByVal hwnd As Long)
Dim hn As Long
Dim firsthd As Long
Dim s As String, t As String
firsthd = GetWindow(hwnd, GW_CHILD)
firsthd = GetWindow(firsthd, GW_HWNDFIRST)
hn = firsthd
Do While hn <> 0
s = String(256, Chr(0))
GetClassName hn, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hn, t, 255
t = Replace(t, Chr(0), "")
Text1.Text = Text1.Text & "句柄:" & hn & " 父句柄:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
TreeView1.Nodes.Add "k" & hwnd, tvwChild, "k" & hn, "句柄:" & hn & " 类名:" & s & "标题:" & t
EnumAllHandles hn
hn = GetWindow(hn, GW_HWNDNEXT)
If hn = firsthd Then Exit Do
Loop
End Sub
Private Sub List1_Click()
If List1.ListIndex = -1 Then Exit Sub
TreeView1.Nodes.Clear
TreeView1.Nodes.Add , , "k" & Trim(Str(Val(Mid(List1.Text, 4)))), List1.Text
Text1.Text = ""
EnumAllHandles Val(Mid(List1.Text, 4))
TreeView1.Nodes("k" & Trim(Str(Val(Mid(List1.Text, 4))))).Expanded = True
End Sub
'添加两个按钮一个文本框一个列表框和一个树形图
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () 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 Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Const GW_CHILD = 5
Private Sub Command1_Click()
Call List1_Click
End Sub
Private Sub Command2_Click()
Dim hwnd As Long
Dim s As String, t As String
List1.Clear
hwnd = GetDesktopWindow()
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1.AddItem "桌面:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
hwnd = GetWindow(hwnd, GW_CHILD Or GW_HWNDFIRST)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1.AddItem "窗口:" & hwnd & " 类名:" & s & " 标题:" & t & vbCrLf
While hwnd <> 0
hwnd = GetWindow(hwnd, GW_HWNDNEXT)
s = String(256, Chr(0))
GetClassName hwnd, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hwnd, t, 255
t = Replace(t, Chr(0), "")
List1.AddItem "窗口:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
Wend
End Sub
Private Sub Form_Load()
Command1.Caption = "获取所有控件"
Command2.Caption = "遍历所有窗体"
End Sub
Private Sub EnumAllHandles(ByVal hwnd As Long)
Dim hn As Long
Dim firsthd As Long
Dim s As String, t As String
firsthd = GetWindow(hwnd, GW_CHILD)
firsthd = GetWindow(firsthd, GW_HWNDFIRST)
hn = firsthd
Do While hn <> 0
s = String(256, Chr(0))
GetClassName hn, s, 255
s = Replace(s, Chr(0), "")
t = String(256, Chr(0))
GetWindowText hn, t, 255
t = Replace(t, Chr(0), "")
Text1.Text = Text1.Text & "句柄:" & hn & " 父句柄:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf
TreeView1.Nodes.Add "k" & hwnd, tvwChild, "k" & hn, "句柄:" & hn & " 类名:" & s & "标题:" & t
EnumAllHandles hn
hn = GetWindow(hn, GW_HWNDNEXT)
If hn = firsthd Then Exit Do
Loop
End Sub
Private Sub List1_Click()
If List1.ListIndex = -1 Then Exit Sub
TreeView1.Nodes.Clear
TreeView1.Nodes.Add , , "k" & Trim(Str(Val(Mid(List1.Text, 4)))), List1.Text
Text1.Text = ""
EnumAllHandles Val(Mid(List1.Text, 4))
TreeView1.Nodes("k" & Trim(Str(Val(Mid(List1.Text, 4))))).Expanded = True
End Sub
'添加两个按钮一个文本框一个列表框和一个树形图
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询