
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
'添加两个按钮一个文本框一个列表框和一个树形图
哥们我用C++做过这样的程序,VB也一样
,你可以在你要控制的程序的文本框中先输入指定的问题假如“ABC”,用EnumChildWindows可以遍历出该句柄下所有控件的句柄,再用GetWindowText取出每个控件
句柄的文本参数,发现为"ABC"就返回该控件句柄
句柄找到了,用SendMessage(Handle,WM_SETTEXT)可以设置你想做的任意值
如果想做美观的msgbox窗口,可以自己用form来做,可以加背景图等效果。
如果想对其它程序d出的msgbox进行 *** 作。可用API函数 findWindow 查找上关标题的窗口,此函数返回值就是hwnd(句柄)
获取句柄不一定要用 FindWindow ,还可以枚举:
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) 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 Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
Private Sub Command1_Click()
Dim lngHwnd As Long, ret As Long
Dim s As String 255
Dim sName As String
lngHwnd = GetWindow(Mehwnd, GW_HWNDFIRST)
Do While lngHwnd
ret = GetWindowText(lngHwnd, s, 255)
sName = Blank(s)
If InStr(sName, "记事本") <> 0 Then '这里“记事本”替换成程序标题相同的部分
'这里写 将句柄加入数组的相关代码
End If
lngHwnd = GetWindow(lngHwnd, GW_HWNDNEXT)
Loop
End Sub
Public Function Blank(ByVal szString As String) As String
Dim l As Integer
l = InStr(szString, Chr(0))
If l > 0 Then
Blank = Left(szString, l - 1)
Else
Blank = szString
End If
End Function
找窗体的句柄得用到API了,最常用的是:FindWindow(一般只找父窗口句柄),FindWindowEx(可找子窗口的句柄),给个例子看看,只用到一个command控件,希望可以帮到你,不了解可以再咨询:Option Explicit
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 GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch 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 Const MaxControlUnit = 65535
Private Sub Command1_Click()
Dim hwnd As Long
Dim hWnd2 As Long
Dim Caption As String 255
Dim ClassName As String 255
Dim Output As String
Dim i As Long
hwnd = FindWindow(vbNullString, "Form1")
For i = 0 To MaxControlUnit
hWnd2 = FindWindowEx(hwnd, hWnd2, vbNullString, vbNullString)
If hWnd2 > 0 Then
GetClassName hWnd2, ClassName, Len(ClassName)
GetWindowText hWnd2, Caption, Len(Caption)
Output = "句柄是:" & hWnd2 & " " & "类名为:" & ClassName
MsgBox Output
Output = "标题是:" & Caption
MsgBox Output
Else
Exit For
End If
Next
End Sub
只要修改那个form1,改成你要的窗体名,就可以了
我当初也做过这个,好像是6年前的事了,记不太清楚了,好像如果便利目标IE的子窗口是找不到input类名也抓不到句柄的,不过我真的记不清了。但下拉框是绝对能找到的。
而且我刚用Spy++试了一下,也没抓到!
好久不玩VB+API了,
如果你就是想学习一下API,那么下面这个是我刚从我Blog里翻出来的
禁用Windows的开始按钮
'
' Paste this into a Code Mode (BAS)
'
Option Explicit
'
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 EnableWindow Lib "user32" (byval hwnd as Long, _
byval fEnable as Long) as Long
public Sub EnableStartMenuButton(byval bEnable as Boolean)
'
' Don't forget to re-enable it !
'
Dim lHwnd as Long
'
lHwnd = FindWindowEx(0&, 0&, "Shell_TrayWnd", vbNullString)
lHwnd = FindWindowEx(lHwnd, 0&, "Button", vbNullString)
Call EnableWindow(lHwnd, bEnable)
'
End Sub
你拿去看看,有点帮助!
Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Sub GetHandle() Dim Result As Long Resule = FindWindow(vbNullString, "迅雷看看播放器") '根据窗口标题取得窗体句柄 GetHandle = Result '返回句柄End Sub只是简单写了一下,你可以根据自己的要求改一下
刚才写错了。应该是Option Explicit Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer Private Sub GetHandle() Dim Result As Integer Resule = FindWindow(vbNullString, "迅雷看看播放器") '根据窗口标题取得窗体句柄 GetHandle = Result '返回句柄End Sub
以上就是关于vb中 如何获得窗体中所有控件的句柄全部的内容,包括:vb中 如何获得窗体中所有控件的句柄、vb 知道窗口句柄以及怎样获得窗口上文本框和按钮句柄并传递信息、vb程序中获取Msgbox窗体的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)