vb如何获取到窗口的句柄

vb如何获取到窗口的句柄,第1张

获取句柄不一定要用 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

'-----------Begin of Module1bas-----------------------------

Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam 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 GetForegroundWindow Lib "user32" () As Long

Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean

Dim s As String

Dim t As String

s = String(1024, Chr(0))

GetWindowText hwnd, s, 1024

t = Trim(Left(s, InStr(1, s, Chr(0)) - 1))

If t <> "" Then Form1List1AddItem t

EnumWindowsProc = True

End Function

'-----------End of Module1bas-----------------------------

'-----------Begin of form1frm-----------------------------

Private Sub Command1_Click()

List1Clear

Call EnumWindows(AddressOf EnumWindowsProc, 0)

Call EnumWindowsProc(GetForegroundWindow, 0)

End Sub

Private Sub Form_Load()

Command1Caption = "开始"

End Sub

'-----------End of form1frm-----------------------------

测试过的,没问题,要源码请发邮件到284304241@qqcom索要,或者到>

OptionExplicitPrivateDeclareFunctionFindWindowLibuser32AliasFindWindowA(ByVallpClassNameAsString,ByVallpWindowNameAsString)AsLongPrivateDeclareFunctionGetWindowTextLibuser32AliasGetWindowTextA(ByValhwndAsLong,ByVallpStringAsString,ByValcchAsLong)AsLongPrivateDeclareFunctionGetWindowLibuser32(ByValhwndAsLong,ByValwCmdAsLong)AsLongPrivateDeclareFunctionGetDesktopWindowLibuser32()AsLongPrivateDeclareFunctionGetClassNameLibuser32AliasGetClassNameA(ByValhwndAsLong,ByVallpClassNameAsString,ByValnMaxCountAsLong)AsLongPrivateConstGW_HWNDFIRST=0PrivateConstGW_HWNDNEXT=2PrivateConstGW_CHILD=5PrivateSubCommand1_Click()CallList1_ClickEndSubPrivateSubCommand2_Click()DimhwndAsLongDimsAsString,tAsStringList1Clearhwnd=GetDesktopWindow()s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem桌面:&hwnd&类名:&s&标题:&t&vbCrLfhwnd=GetWindow(hwnd,GW_CHILDOrGW_HWNDFIRST)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWhilehwnd<0hwnd=GetWindow(hwnd,GW_HWNDNEXT)s=String(256,Chr(0))GetClassNamehwnd,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthwnd,t,255t=Replace(t,Chr(0),)List1AddItem窗口:&hwnd&类名:&s&标题:&t&vbCrLfWendEndSubPrivateSubForm_Load()Command1Caption=获取所有控件Command2Caption=遍历所有窗体EndSubPrivateSubEnumAllHandles(ByValhwndAsLong)DimhnAsLongDimfirsthdAsLongDimsAsString,tAsStringfirsthd=GetWindow(hwnd,GW_CHILD)firsthd=GetWindow(firsthd,GW_HWNDFIRST)hn=firsthdDoWhilehn<0s=String(256,Chr(0))GetClassNamehn,s,255s=Replace(s,Chr(0),)t=String(256,Chr(0))GetWindowTexthn,t,255t=Replace(t,Chr(0),)Text1Text=Text1Text&句柄:&hn&父句柄:&hwnd&类名:&s&标题:&t&vbCrLfTreeView1NodesAddk&hwnd,tvwChild,k&hn,句柄:&hn&类名:&s&标题:&tEnumAllHandleshnhn=GetWindow(hn,GW_HWNDNEXT)Ifhn=firsthdThenExitDoLoopEndSubPrivateSubList1_Click()IfList1ListIndex=-1ThenExitSubTreeView1NodesClearTreeView1NodesAdd,,k&Trim(Str(Val(Mid(List1Text,4)))),List1TextText1Text=EnumAllHandlesVal(Mid(List1Text,4))TreeView1Nodes(k&Trim(Str(Val(Mid(List1Text,4)))))Expanded=TrueEndSub'添加两个按钮一个文本框一个列表框和一个树形图

以上就是关于vb如何获取到窗口的句柄全部的内容,包括:vb如何获取到窗口的句柄、VB获取所有运行程序的句柄、vb中 如何获得窗体中所有控件的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10175406.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存