VB向一个外部程序发送一条信息(回车键)
首先在窗体上添加一个command1和一个text1PrivateDeclareFunctionFindWindowLib"user32"Alias"FindWindow...
首先在窗体上添加一个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的,但现在我想发送回车键。请问如何是好呢?谢谢了。 展开
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的,但现在我想发送回车键。请问如何是好呢?谢谢了。 展开
2个回答
展开全部
实际上不必用费劲的常数,模拟按键都是PostMessage
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 PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Sub Command1_Click()
Dim a As Long
a = FindWindow(vbNullString, Text1.Text)
PostMessage a, WM_KEYDOWN, vbKeyReturn, 0
End Sub
keybd_event也可以用这种方法
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 PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Const WM_KEYDOWN = &H100
Private Const WM_KEYUP = &H101
Private Sub Command1_Click()
Dim a As Long
a = FindWindow(vbNullString, Text1.Text)
PostMessage a, WM_KEYDOWN, vbKeyReturn, 0
End Sub
keybd_event也可以用这种方法
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询