VB 如何获取一外部程序窗口内已知控件句柄的内容

VB 如何获取一外部程序窗口内已知控件句柄的内容,第1张

我有个实例:打开记事本+获得记事本的句柄+向记事本中写入字符串+更改记事本的标题+获得所有运行的程序的标题。代码如下:

VERSION 500

Begin VBForm Form1

Caption = "Form1"

ClientHeight = 5325

ClientLeft = 60

ClientTop = 450

ClientWidth = 7560

LinkTopic = "Form1"

LockControls = -1 'True

ScaleHeight = 5325

ScaleWidth = 7560

StartUpPosition = 2 '屏幕中心

Begin VBListBox List1

Height = 4380

Left = 360

TabIndex = 4

Top = 480

Width = 4095

End

Begin VBCommandButton Command4

Caption = "获得所有程序标题"

Height = 495

Left = 4800

TabIndex = 3

Top = 2400

Width = 2535

End

Begin VBCommandButton Command3

Caption = "打开写入记事本+改标题"

Height = 495

Left = 4800

TabIndex = 2

Top = 1800

Width = 2535

End

Begin VBCommandButton Command2

Caption = "运行记事本"

Height = 495

Left = 4800

TabIndex = 1

Top = 1200

Width = 2535

End

Begin VBCommandButton Command1

Caption = "打开写入记事本"

Height = 495

Left = 4800

TabIndex = 0

Top = 600

Width = 2535

End

End

Attribute VB_Name = "Form1"

Attribute VB_GlobalNameSpace = False

Attribute VB_Creatable = False

Attribute VB_PredeclaredId = True

Attribute VB_Exposed = False

'1

Private Declare Function WinExec Lib "kernel32" (ByVal lpCmdLine As String, ByVal nCmdShow As Long) As Long

'==========================================================

'3

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 SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_PASTE = &H302

'===========================================================

'4

Private Declare Function GetDesktopWindow Lib "user32" () As Long

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_CHILD = 5

Private Const GW_HWNDNEXT = 2

'

Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

Private Sub Command1_Click()

'1 打开 写入 记事本

Dim AppID As Integer

AppID = Shell("C:\WINDOWS\notepadexe", 1)

AppActivate AppID

SendKeys "-2356e+012年后光荣感如果好了" '"SendKeys--字符有误,速度慢"

End Sub

Private Sub Command2_Click()

WinExec "notepadexe", 5

End Sub

Private Sub Command3_Click()

'3 打开 写入 记事本+改标题

Dim TemphWnd As Long

AppID = Shell("C:\WINDOWS\notepadexe", 1)

TemphWnd = FindWindow("Notepad", vbNullString)

'--------------------------------------------

SetWindowText TemphWnd, "Welcome to VB" '

'--------------------------------------------

TemphWnd = FindWindowEx(TemphWnd, 0, "Edit", vbNullString)

If TemphWnd Then

'DebugPrint TemphWnd

VBClipboardSetText "-2356e+012年后光荣感如果好了" '"SendMessage--字符正确,速度快"

SendMessage TemphWnd, WM_PASTE, 0, ByVal 0&

End If

End Sub

Private Sub Command4_Click()

'4 获得所有程序标题

Dim ret As Long

Dim hwnd As Long

Dim str As String 256

ret = GetDesktopWindow()

hwnd = GetWindow(ret, GW_CHILD)

Do While hwnd <> 0

GetWindowText hwnd, str, Len(str)

hwnd = GetWindow(hwnd, GW_HWNDNEXT)

If Left$(str, 1) <> vbNullChar Then

Form1List1AddItem str

End If

Loop

End Sub

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 Sub Command1_Click()

Dim ss As Long

Dim sb As Long

ss = FindWindow(vbNullString, "找控件")

If ss = 0 Then Exit Sub '防止主为0 时查找桌面子窗口

Do

sb = FindWindowEx(ss, sb, vbNullString, vbNullString) '获取控件句柄

Print sb

Loop While sb > 0

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:

GetForegroundWindow

可以获取到有焦点的顶层窗口

获取有焦点的子窗口只能用GetFocus,但是如果当前顶层窗口属于其他线程,GetFocus将返回NULL。没有好的办法解决,除非AttachThreadInput或者用钩子(HOOK)

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

'这是定义获得窗口句柄的API函数

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long,lParam As Any) As Long '这句是定义向获得窗口句柄发送按键消息的API函数

例子:你先建一个工程

form1caption="12345

画一个command1 名字为 "确定1"

事件

Private Sub Command1_Click()

msgbox MeHwnd '显示确定按钮的句柄

End Sub

生成12345exe

关闭VB6

打开VB6

再建一个工程 代码如下:

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 PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Private Const WM_LBUTTONDOWN = &H201

Private Const WM_LBUTTONUP = &H202

Private Const MK_LBUTTON = &H1

Dim hpwnd As Long, hcwnd As Long

Dim iResult As Long

Private Sub Command1_Click()

hpwnd = FindWindow(vbNullString, "12345")

hcwnd = FindWindowEx(hpwnd, 0, vbNullString, "确定")

SetForegroundWindow hcwnd

iResult = SendMessage(hcwnd, WM_LBUTTONDOWN, 0, 0&)

iResult = SendMessage(hcwnd, WM_LBUTTONUP, 0, 0&)

End Sub

然后先运行 事先生成好的12345exe

再运行后建的那个

你单击确定 就会使12345exe的确定按钮被单击 返回它的hwnd

我的方法是先找到12345exe的主窗口 然后根据主窗口的句柄获得其确定按钮的句柄 再使用sendmessage 发送模拟鼠标左键单击

VB控件的句柄 一般可用mehwnd 方法获得

以上就是关于VB 如何获取一外部程序窗口内已知控件句柄的内容全部的内容,包括:VB 如何获取一外部程序窗口内已知控件句柄的内容、VB FindWindowEx 怎么找出 窗体控件的句柄、VB 如何得到窗体内控件的句柄等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址: http://outofmemory.cn/web/9279180.html

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

发表评论

登录后才能评论

评论列表(0条)

保存