VB 找到两个程序的句柄并执行一下操作
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VB 找到两个程序的句柄并执行一下操作相关的知识,希望对你有一定的参考价值。
这个问题有点怪,本人技术相当低,
研究了很多天,实在做不出来~~帮帮我吧....
假设有个名字叫“12345”的程序,其中有个名为“确定”的按钮,
将这个名为“12345”程序打开两个
(双击后出来一个,再双击又出来一个,是两个,不是一个),
将这两个程序显示(显示名字或句柄都可以)在两个按钮上
单击按钮,就可以对相应的程序中的“确定”按钮进行单击
VB源码...谢谢....
打个比方:
好比在游戏中刷分,刷分需要打开两个游戏,刷分途径就是单击“开始”按钮,那个游戏叫做“123456”。
用VB实现控制游戏中按钮的单击:
将这两个程序的名字或句柄分别显示在两个按钮上(取代list),
通过单击这两个按钮,可以来实现对“123456”程序中的“确定”按钮的单击
写成“start1”了,代码已修改~~
添加:
两个长点的按钮:Command1 Command2 用于显示句柄
再两个按钮Command3 Command4
无法实现当打开两个程序后分别将它们的句柄显示出来~~所以在打开第一个程序后再打开它,打开第二个后点 Command4(刷新)单击那两个按钮其中一个就可以实现单击
'---------------------------------获得窗体句柄定义
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Dim c As Long,b 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 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
Dim start1 As Long,start2 As Long
'-----------------------------------
Private Sub Form_Load()
c = FindWindow(vbNullString, "12345")'寻找“12345”
Command1.Caption = "程序句柄:" & c
Command3.Caption = "退出"
Command4.Caption = "刷新"
start1 = FindWindowEx(c, 0, vbNullString, "确定")
'第二个程序中的“确定”
End Sub
Private Sub Command4_Click()
b = FindWindow(vbNullString, "12345")
Command2.Caption = "程序句柄:" & b
start2 = FindWindowEx(b, 0, vbNullString, "确定")
'第二个程序中的“确定”
End Sub
Private Sub Command1_Click()
SendMessage start1, &HF5, 0, 0
End Sub
Private Sub Command2_Click()
SendMessage start2, &HF5, 0, 0
End Sub
Private Sub Command3_Click()
End
End Sub 参考技术A 你说的我有点晕啊 不是很清楚
你先建一个工程
form1.caption="12345
画一个command1 名字为 "确定1"
事件
Private Sub Command1_Click()
msgbox Me.Hwnd '显示确定按钮的句柄
End Sub
生成12345.exe
关闭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
然后先运行 事先生成好的12345.exe
再运行后建的那个
你单击确定 就会使12345.exe的确定按钮被单击 返回它的hwnd
我的方法是先找到12345.exe的主窗口 然后根据主窗口的句柄获得其确定按钮的句柄 再使用sendmessage 发送模拟鼠标左键单击
VB控件的句柄 一般可用me.hwnd 方法获得 参考技术B 你是说用一个程序控制另一个程序中的控件吧?
首先用FindWindowEx或者Findwindos找到窗体的句柄
然后用SendMessage函数发送BN_CLICKED消息
代码:
WM_HWND=FindWindow(vbNULLString,"12345")
SendMessage WM_HWND,BN_CLICKED,0,0
关于BN_CLICKED的值,你可以自己动手查一下,也能加强一下对windows内部消息传递的认识 参考技术C 我总算理解明白了~ 参考技术D 什么啊?
显示名称在两个按纽上吗?
你怎么开都知道了还不会获得它的文件路径么..
以上是关于VB 找到两个程序的句柄并执行一下操作的主要内容,如果未能解决你的问题,请参考以下文章
vb 知道窗口句柄以及怎样获得窗口上文本框和按钮句柄并传递信息