vb句柄视频教程(vb 获取如何自己子控件的句柄)

本文目录
vb 获取如何自己子控件的句柄
新建一个工程,添加两个lable,一个command,一个timer
复制以下代码,再生成EXE文件,打开那个文件,即可获取鼠标所在的坐标与句柄
代码如下:
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
’返回包含了指定点的窗口的句柄
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Type POINTAPI
x As Long
y As Long
End Type
Dim p As POINTAPI
Private Sub Command1_Click()
GetCursorPos p
Dim hwnd As Long
Dim pt As POINTAPI
GetCursorPos pt ’取得鼠标坐标信息
hwnd = WindowFromPoint(p.x, p.y) ’获取句柄
If MsgBox("X坐标: " & p.x & " ,Y坐标:" & p.y & " 鼠标指向句柄:" & hwnd & "", vbOKOnly, "查看句柄与坐标") = 6 Then
End If
End Sub
Private Sub Form_Load()
Timer1.Timer = 5
GetCursorPos p
Dim hwnd As Long
Dim pt As POINTAPI
GetCursorPos pt ’取得鼠标坐标信息
hwnd = WindowFromPoint(p.x, p.y) ’获取句柄
Me.Caption = "坐标与窗口句柄查看器"
Label3.Caption = "X坐标:" & p.x & ",Y坐标:" & p.y & ""
Label1.Caption = " 当前指向窗口句柄: " & hwnd & ""
Command1.Caption = "获取鼠标指向句柄与坐标(&H)"
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub timer1_timeR()
GetCursorPos p
Dim hwnd As Long
Dim pt As POINTAPI
GetCursorPos pt ’取得鼠标坐标信息
hwnd = WindowFromPoint(p.x, p.y) ’获取句柄
Label1.Caption = " 当前指向窗口句柄: " & hwnd & ""
Label3.Caption = "X坐标: " & p.x & " ,Y坐标:" & p.y & ""
End Sub
VB如何获得相应窗体的句柄和按钮
方法1:
窗体名句柄 如:frmStu.hwnd
按钮标题: Command1.Caption
方法2:
Dim hwnd As Long
Dim str1 As String, len1 As Long
’hwnd = FindWindowa("ThunderRT6FormDC", "Form1") ’编译成EXE用这句,
hwnd = FindWindowa("ThunderFormDC", "Form1") ’在调试的时候用这句.
str1 = Space(255) ’定义接收字串.
GetWindowText hwnd, str1, 1024
Do While hwnd 《》 0
hwnd = GetNextWindow(hwnd, 2) ’只有2才表示找下一个窗口
len1 = GetWindowText(hwnd, str1, Len(str1))
If (InStr(1, str1, "API", 1) 》 0) Then
Exit Do
End If
Loop
SetWindowText hwnd, "我把API阅读器的标题改变了"
hwnd = GetNextWindow(hwnd, 5) ’5表示子窗口
Do While hwnd 《》 0
hwnd = GetNextWindow(hwnd, 2) ’只有2才表示找下一个窗口
GetWindowText hwnd, str1, Len(str1)
SetWindowText hwnd, "我改变了改钮2"
Loop
要在模块里定义:
Public Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag 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
Public Declare Function FindWindowa Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function GetForegroundWindow Lib "user32" () As Long
Public Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
Public Declare Function GetDlgItem Lib "user32" (ByVal hDlg As Long, ByVal nIDDlgItem As Long) As Long
如何使用VB搜索句柄,详细如下.
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 MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Sub Command1_Click()
Dim h As Long, x As Long, y As Long
h = FindWindowEx(0, 0, "Notepad", vbNullString)
Do Until h = 0
MoveWindow h, x, y, 200&, 200&, 0
x = x + 200
If x 》= 800 Then
x = 0
y = y + 200
End If
h = FindWindowEx(0, h, "Notepad", vbNullString)
Loop
End Sub
vb 获取窗口句柄
’创建一个列表框和一个按钮
’API函数定义
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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow 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
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Private Sub Command1_Click()
Dim s As Long
Dim a As String
a = Space(255)
s = GetWindow(Me.hwnd, GW_HWNDFIRST) ’获得句柄
GetWindowText s, a, 255 ’获得标题
Dim ab As String * 255
GetClassName s, ab, 255 ’获得类名
Dim ClassName As String
ClassName = StripTerFlag(ab) ’StripTerFlag是一个自定义函数,后面有代码,用于去处结束符
If UCase(ClassName) = "NOTEPAD" Then
List1.AddItem a
List1.ItemData(List1.ListCount - 1) = s
End If
Do While s
DoEvents
s = GetWindow(s, GW_HWNDNEXT) ’获得句柄
GetWindowText s, a, 255 ’获得标题
GetClassName s, ab, 255 ’获得类名
ClassName = StripTerFlag(ab)
If UCase(ClassName) = "NOTEPAD" Then
List1.AddItem a
List1.ItemData(List1.ListCount - 1) = s
End If
Loop
End Sub
Private Sub List1_Click()
MsgBox List1.ItemData(List1.ListIndex) ’单击列表框获得句柄
End Sub
VB获取窗口句柄的详细教程
1、首先在电脑中打开VB软件,然后点击窗口。
2、然后找到窗体布局窗口,就可以在这里,直接拖曳鼠标,设置窗体的位置。
3、如果没有默认打开,回到操作页面中,找到并点击如下图的按钮。
4、也可以找到stateUpPosition这个属性。
5、然后在打开点页面中,就可以根据自己的需要,设置预设的类型了。
vb怎么获取控件句柄和内容
先找到窗口的句柄,再来查找按钮的句柄,如果按钮有文本内容,那就好办,如果没有,那建议你利用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 w.GetWindowHwndByTitleEx("自动化操作框架") 》 0 Then
i = i + 1
Do While w.GetElementHwndByClassName("ThunderCommandButton", i) 》 0’按次序遍历
MsgBox w.GetElementHwndByClassName("ThunderCommandButton", i)’得到当前次序按钮的句柄
w.SetElementTextByClassName "ThunderCommandButton", "次序" & i, i’设置按钮文本
i = i + 1
Loop
End If
End Sub
怎样用VB抓取窗口的句柄
找窗体的句柄得用到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,改成你要的窗体名,就可以了

更多文章:
知名女星孙侨璐于跨年夜,心梗猝死,年仅25岁!一一一写给年轻人?1月2日演员孙侨潞离世,说好的来日方长转眼就是别离
2026年10月15日 10:40
avantage怎么设置为中文(Avantage安装丢失GIT)
2026年10月15日 03:00
activex控件未注册怎么办(登陆视频监控网页时提示activex控件未注册,怎么解决)
2026年10月14日 20:30







