
答案是不需要获取句柄,就能实现
方法是把第2个聊天窗口或第3个的内容传输到第1个窗口内显示,再改变窗口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
'添加两个按钮一个文本框一个列表框和一个树形图
代码我不写了,太长了,具体思路如下:
整体是用WINDOWS API
1、findwindow 找到指定标题的窗口
如果这个窗口标题你不知道,那么一个一个遍历吧,或者枚举所有进程(进程名字你总是知道的吧?)然后在遍历整个进程中所有的窗口。
通过以上的方法,可以定位到窗口
2、GetWindow 到这个窗口里找控件
使用 GW_CHILD 常数,这样找到的就是这个窗体的子窗体(控件)的句柄了。
但是,有时候,控件是多层嵌套的,比如:窗口里有一个frame,frame里又嵌入一个frame,然后里面是一个textbox。
这样是很常见的,那么唯一的方案就是用 getwindow递归查找GW_CHILD,如果GW_CHILD返回的是0,那么就说明没有子窗体,通过递归查找,肯定就能找到你要的控件的句柄
3、发送消息
找到以后,getwindowtext、sendmessage就可以用了,WM_GETTEXT可以获得文本框文字,但是,不一定所有控件都响应这个消息,比如QQ密码框,这些加密的控件,除非你写驱动,否则恐怕很难去在这些控件上拦截、发送消息
找窗体的句柄得用到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,改成你要的窗体名,就可以了
以上就是关于VB 如何获取层叠式窗体的各个子窗体的标题和句柄全部的内容,包括:VB 如何获取层叠式窗体的各个子窗体的标题和句柄、vb中 如何获得窗体中所有控件的句柄、VB 如何得到窗体内控件的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)