vb中 如何获得窗体中所有控件的句柄

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

List1Clear

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), "")

List1AddItem "桌面:" & 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), "")

List1AddItem "窗口:" & 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), "")

List1AddItem "窗口:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf

Wend

End Sub

Private Sub Form_Load()

Command1Caption = "获取所有控件"

Command2Caption = "遍历所有窗体"

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), "")

Text1Text = Text1Text & "句柄:" & hn & " 父句柄:" & hwnd & " 类名:" & s & "标题:" & t & vbCrLf

TreeView1NodesAdd "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 List1ListIndex = -1 Then Exit Sub

TreeView1NodesClear

TreeView1NodesAdd , , "k" & Trim(Str(Val(Mid(List1Text, 4)))), List1Text

Text1Text = ""

EnumAllHandles Val(Mid(List1Text, 4))

TreeView1Nodes("k" & Trim(Str(Val(Mid(List1Text, 4)))))Expanded = True

End Sub

'添加两个按钮一个文本框一个列表框和一个树形图

这个函数返回了CWinThread指针,它的一个成员m_hThread就是线程的句柄再次执行你就要再调用AfxBeginThread,因为如果线程结束了,那你得到的句柄其实已经无效了

首先你要知道 你要找的是进程的句柄。句柄是 *** 作系统 *** 作的对象,任何一个对象比如一个textbox 也有他的句柄,不一定只要窗口才有句柄。现在你要找到进程的句柄,貌似用C#的话必须要用API 可以用Delphi 试试 这样比较方便 比如 FindWindow 我没有尝试用 这个函数来找 隐藏窗体的句柄 但是 一般 找窗体都没问题的。

以上就是关于vb中 如何获得窗体中所有控件的句柄全部的内容,包括:vb中 如何获得窗体中所有控件的句柄、MFC中怎样从一个线程中获取当前主窗口的句柄、C# 根据进程ID获取进程主窗口句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9409298.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存