
'创建一个列表框和一个按钮
'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(mehwnd,
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
list1additem
a
list1itemdata(list1listcount
-
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
list1additem
a
list1itemdata(list1listcount
-
1)
=
s
end
if
loop
end
sub
private
sub
list1_click()
msgbox
list1itemdata(list1listindex)
'单击列表框获得句柄
end
sub
获取句柄不一定要用 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
新建一个工程,添加两个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(px, py) '获取句柄
If MsgBox("X坐标: " & px & " ,Y坐标:" & py & " 鼠标指向句柄:" & hwnd & "", vbOKOnly, "查看句柄与坐标") = 6 Then
End If
End Sub
Private Sub Form_Load()
Timer1Timer = 5
GetCursorPos p
Dim hwnd As Long
Dim pt As POINTAPI
GetCursorPos pt '取得鼠标坐标信息
hwnd = WindowFromPoint(px, py) '获取句柄
MeCaption = "坐标与窗口句柄查看器"
Label3Caption = "X坐标:" & px & ",Y坐标:" & py & ""
Label1Caption = " 当前指向窗口句柄: " & hwnd & ""
Command1Caption = "获取鼠标指向句柄与坐标(&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(px, py) '获取句柄
Label1Caption = " 当前指向窗口句柄: " & hwnd & ""
Label3Caption = "X坐标: " & px & " ,Y坐标:" & py & ""
End Sub
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
'添加两个按钮一个文本框一个列表框和一个树形图
以上就是关于VB 获取指定窗口标题和句柄全部的内容,包括:VB 获取指定窗口标题和句柄、vb如何获取到窗口的句柄、vb 获取如何自己子控件的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)