vb怎么获取控件句柄和内容

vb怎么获取控件句柄和内容,第1张

 先找到窗口句柄,再来查找按钮的句柄,如果按钮有文本内容,那就好办,如果没有,那建议你利用SPY++先来察看一下按钮的类型,以此类型为查找参数多次调用FindWindowEx来查找,直到找到的的句柄和Spy++相同,那么这个按钮就找到了。

按钮类名同样,但是你去遍历的时候它的次序始终是固定的。通过id不可靠,有些有id但是有些id是0。

clswindow类,里面有个函数etElementHwndByClassName,可以得到指定的次序按钮。加入你要处理的按钮是在第二个次序,类名为Button,那么就用:GetElementHwndByClassName("Button",2),即可,

具体代码:

Private Sub Command6_Click()

Dim w As New clsWindow

Dim i%

If wGetWindowHwndByTitleEx("自动化 *** 作框架") > 0 Then

i = i + 1

Do While wGetElementHwndByClassName("ThunderCommandButton", i) > 0'按次序遍历

MsgBox wGetElementHwndByClassName("ThunderCommandButton", i)'得到当前次序按钮的句柄

wSetElementTextByClassName "ThunderCommandButton", "次序" & i, i'设置按钮文本

i = i + 1

Loop

End If

End Sub

Private Const GW_HWNDNEXT = 2

Private Const GW_HWNDFIRST = 0

Private Const WM_GETTEXT = &HD

Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd 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 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

Private Function GetWindowList() As String

Dim hwnd As Long

Dim s As String

hwnd = Mehwnd

hwnd = GetWindow(hwnd, GW_HWNDFIRST)

While hwnd <> 0

s = String(256, Chr(0))

GetClassName hwnd, s, 255

s = Replace(s, Chr(0), "")

If s = "CabinetWClass" Then

GetWindowList = GetWindowList & GetUrl(hwnd) & vbCrLf

End If

hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)

Wend

End Function

Private Function GetUrl(hwnd As Long) As String

Dim NexthWnd As Long

Dim s As String

NexthWnd = 0

NexthWnd = FindWindowEx(hwnd, NexthWnd, vbNullString, vbNullString)

While NexthWnd <> 0

s = String(256, Chr(0))

GetClassName NexthWnd, s, 255

s = Replace(s, Chr(0), "")

If s = "Edit" Then

s = String(256, Chr(0))

SendMessage NexthWnd, WM_GETTEXT, 255, s

s = Replace(s, Chr(0), "")

GetUrl = s

If Len(s) > 0 Then Exit Function

Else

GetUrl = GetUrl(NexthWnd)

If Len(GetUrl) > 0 Then Exit Function

End If

NexthWnd = FindWindowEx(hwnd, NexthWnd, vbNullString, vbNullString)

Wend

End Function

Private Sub Command1_Click()

text1text=GetWindowList

End Sub

FindWindowEx 函数可根据类名查找窗口得到句柄,你可以用 SendMessage 给该控件发送消息来执行各种 *** 作。但是,如果该控件是 Windows 标准控件或者由标准控件扩展而来的,比如 Edit 类可以通过发送 WM_GETTEXT 获取文字,同时其他类似的比如 Delphi 的 TEdit 控件同样也可以获取。而有些非微软标准第三方控件,他们大多扩展了功能,对原来的消息肯定是不响应的,而是使用他们自定义的消息或者函数来设置数据,自定义结构体保存数据,那么你无从获知获取该控件内容的方法。

'因为还有一层"常规"的"#32770"才到"Edit"

'完整代码如下

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Sub Form_Load()

WinH = FindWindow("#32770", vbNullString)

Dim Wintxt As String 256

Do While WinH <> 0

SendMessage WinH, WM_GETTEXT, ByVal 256, ByVal Wintxt

If InStr(Wintxt, "属性") Then

a = FindWindowEx(WinH, 0&, "#32770", "常规")

If a <> 0 Then

b = FindWindowEx(a, 0&, "Edit", vbNullString)

b = FindWindowEx(a, b, "Edit", vbNullString)

b = FindWindowEx(a, b, "Edit", vbNullString)

b = FindWindowEx(a, b, "Edit", vbNullString)

SendMessage b, &HD, ByVal 256, ByVal Wintxt

Exit Do

End If

End If

WinH = FindWindowEx(0, WinH, "#32770", vbNullString)

DoEvents

Loop

MsgBox Replace(Wintxt, Chr(0), "")

End Sub

首先,一个窗口的句柄根本不是固定的,所以用常量来表示一个窗口句柄是不合适的。VB的窗体都有一个名为hWnd的属性,这个属性里保存的就是这个窗口的句柄。

其次,“#32770”是普通窗体的窗口类名,如果要找这样的窗口,可以用handle = FindWindow(窗口类名, 窗口名)来寻找它。

如果要寻找子窗口的句柄,假设父窗口的句柄为phandle,那么可以用chandle = FindWindowEx(phandle, 0, 子窗口类名, vbNullString)来完成。此时,找到的是父窗口内第一个类名为“子窗口类名”的子窗口。如果要找到下一个子窗口,需要用chandle2 = FindWindowEx(phandle, chandle, 子窗口类名, vbNullString)来完成。

现在,已经知道了第一个子窗口的句柄为h1,子窗口类名为"EDIT",那么我们可以用如下语句来寻找第二个子窗口:

h2 = FindWindowEx(874523, h1, "EDIT", vbNullString)

不过还是建议用一个变量来保存父窗口的句柄。顺便说一下,如果此时h2为0,那就是说,这个父窗口内没有第二个类名为"EDIT"的子窗口了。

以上就是关于vb怎么获取控件句柄和内容全部的内容,包括:vb怎么获取控件句柄和内容、VB如何获取窗口地址栏内容、如何得到其他进程里MSFlexGridWndClass表格控件里的内容等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/web/9316305.html

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

发表评论

登录后才能评论

评论列表(0条)

保存