vb 获取窗口上多个控件的句柄,如何知道哪个是自己想要的?

一个程序窗口上有好几个"Edit",,如何才能找到我想要的那个"Edit", 呢?
或者模拟鼠标操作“计算器”上门的按键都是“Button”。如何才能确定我要的1、2、3、4、5、6、7、8、9、这些按键的句柄呢?
往高手讲两句。谢谢了。
下面是记事本改编的代码。参考用。
Private Sub Command1_Click()
Dim WindowHandle As Long, ChildWindowHandle As Long
WindowHandle = FindWindow(vbNullString, "工程1")

If WindowHandle Then '如果获取句柄成功
ChildWindowHandle = FindWindowEx(WindowHandle, 0, "Edit", vbNullString)

If ChildWindowHandle Then '如果成功获取子句柄
SendMessage ChildWindowHandle, WM_SETTEXT, 0, ByVal CStr("hello") '发送hello消息
Else
MsgBox "无法获取子窗口", vbCritical, "提示"
End If
Else
MsgBox "无法获取窗口", vbCritical, "提示"
End If
End Sub

第1个回答  2011-04-02
这个可能是你想要的东东吧!
=========================
VB 遍历窗口所有子窗体句柄

Private Const GW_CHILD = 5
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2

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 GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Private Declare Function IsWindow Lib "user32" (ByVal hwnd 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 GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Sub FillChild(hWndParent As Long)
Dim hWndChild As Long
Dim szCaption As String
Dim buffer As String
Dim i As Long

hWndChild = GetWindow(hWndParent, GW_CHILD)
If (hWndChild = 0) Then Exit Sub
hWndChild = GetWindow(hWndChild, GW_HWNDFIRST)
If hWndChild = 0 Then Exit Sub

While (hWndChild <> 0)
szCaption = String$(255, 0)
GetClassName hWndChild, szCaption, 250
szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1)
buffer = CStr(hWndChild) & "--" & szCaption
i = GetWindowTextLength(hWndChild)
szCaption = String$(255, 0)
GetWindowText hWndChild, szCaption, 250
szCaption = Left$(szCaption, i)
buffer = buffer & "--" & szCaption

List1.AddItem buffer
FillChild hWndChild
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
Wend
End Sub

Private Sub GetChildWindow(hwnd As Long)
Dim szCaption As String
Dim buffer As String

Dim i As Long

List1.Clear
szCaption = String$(255, 0)
GetClassName hwnd, szCaption, 250
szCaption = Left$(szCaption, InStr(szCaption, String$(1, 0)) - 1)

buffer = CStr(hwnd)
buffer = buffer & "--" & szCaption
i = GetWindowTextLength(hwnd)
szCaption = String$(255, 0)
GetWindowText hwnd, szCaption, 250
szCaption = Left$(szCaption, i)
buffer = buffer & "--" & szCaption
List1.AddItem buffer
FillChild hwnd
End Sub

调用方法
GetChildWindow hwnd'hwnd是指定的窗口句柄

结果以

窗体句柄--窗体类名称--窗体Text

形式列在列表框List1中本回答被提问者采纳
第2个回答  2011-04-01
窗体枚举,name为Button的Text,或Command的caption
送你一个工具,Spy++,下载运行一目了然。
第3个回答  2011-04-02
一、编程前的准备工作:
1、打开“计算器”程序;
2、用Spy+工具,查看0-9按钮对应的句柄(当然包括其他你想要的句柄);
3、利用VB枚举计算器窗口下的所有子窗体句柄;
4、通过对比,找出0-9句柄在在枚举列表中所处的相对位置,并记住此位置。
二、编程实现
在打开“计算器”程序的前提下,如果你想点击1对应的按钮,就先枚举计算器窗口下的所有子窗体句柄;当找到1所处的相对位置时,此时的Button就可模拟点击了。其他数字的点击实现与此相似。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网