VB向一个外部程序发送一条信息(如发送tab键,回车键)

2024-12-28 11:09:50
推荐回答(1个)
回答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消息