找窗体的句柄得用到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,改成你要的窗体名,就可以了
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
lpClassName参数指向类名,lpWindowName指向窗口名,如果有指定的类名和窗口的名字则表示成功返回一个窗口的句柄。否则返回零。
'下面的代码为获取标题为“QQ”的窗体句柄
'效果看截图
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Sub Command1_Click()
Dim Hwnd As Long
'模糊搜索,返回标题为“QQ”的窗体
Hwnd = FindWindow(vbNullString, "QQ")
If Hwnd <> 0 Then
DebugPrint "获取成功,窗口句柄为:" & Hwnd
Else
DebugPrint "获取失败"
End If
End Sub
Option Explicit Private Declare Function GetDesktopWindow Lib "USER32" () As Long Private Declare Function GetWindow Lib "USER32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Public Declare Function GetWindowText Lib "USER32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Private Const GW_CHILD = 5 Private Const GW_HWNDNEXT = 2 Public Function GetHandle(Title As String) As Long Dim tmp As String Dim hwnd As Long Dim lngProcID As Long Dim strTitle As String 255 '用来存储窗口的标题 '取得桌面窗口 hwnd = GetDesktopWindow() '取得桌面窗口的第一个子窗口 hwnd = GetWindow(hwnd, GW_CHILD) '通过循环来枚举所有的窗口 Do While hwnd <> 0 '取得下一个窗口的标题,并写入到列表框中 GetWindowText hwnd, strTitle, Len(strTitle) If Left$(strTitle, 1) <> vbNullChar Then tmp = Left$(strTitle, InStr(1, strTitle, vbNullChar)) If Left(tmp, Len(Title)) = Title Then GetHandle = hwnd End If End If '调用GetWindow函数,来取得下一个窗口 hwnd = GetWindow(hwnd, GW_HWNDNEXT) Loop End Function
以上就是关于怎样用VB抓取窗口的句柄全部的内容,包括:怎样用VB抓取窗口的句柄、vb6.0 如何使用findwondow获得某窗口的句柄比如下面这些……、VB只输入一半的窗口标题就能获得句柄怎么做等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)