VB向一个外部程序发送一条信息(如发送tab键,回车键)
问题:先有1程序A.exe先用VB写一程序向A发送TAB键,然后在发送Enter键。怎么实现?ps:用sendkeys,keybd_event发生ALT+F4成功,但发送...
问题:先有1程序A.exe 先用VB写一程序向A发送TAB键,然后在发送Enter键。
怎么实现?
ps:用sendkeys,keybd_event发生ALT+F4 成功,但发送tab键没有效果。网上另有种说话用findwindows 取得程序句柄,然后先这句柄发送按键。但资料非常少?求教各位? 展开
怎么实现?
ps:用sendkeys,keybd_event发生ALT+F4 成功,但发送tab键没有效果。网上另有种说话用findwindows 取得程序句柄,然后先这句柄发送按键。但资料非常少?求教各位? 展开
1个回答
展开全部
findwindows是wind32的api函数
寻找符合条件的窗体,返回句柄
下面是我我写的向外部程序发送tab键的程序
首先在窗体上添加一个command1 和 一个text1
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo 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_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_TAB = &H9
Private Sub Command1_Click()
Dim a As Long
a = FindWindow(vbNullString, Text1.Text)
SendMessage a, WM_KEYDOWN, VK_TAB, 0
SendMessage a, WM_KEYUP, VK_TAB, 0
End Sub
说明: 测试时,应先将接受按键消息的程序运行(可后台运行),然后在text1中输入程序窗体名(可程序名)点击command1即可发送tab消息
寻找符合条件的窗体,返回句柄
下面是我我写的向外部程序发送tab键的程序
首先在窗体上添加一个command1 和 一个text1
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo 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_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Const VK_TAB = &H9
Private Sub Command1_Click()
Dim a As Long
a = FindWindow(vbNullString, Text1.Text)
SendMessage a, WM_KEYDOWN, VK_TAB, 0
SendMessage a, WM_KEYUP, VK_TAB, 0
End Sub
说明: 测试时,应先将接受按键消息的程序运行(可后台运行),然后在text1中输入程序窗体名(可程序名)点击command1即可发送tab消息
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询