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的,但现在我想发送回车键。请问如何是好呢?谢谢了。
展开
 我来答
ccccmmmmdddd
2012-02-07 · TA获得超过1189个赞
知道小有建树答主
回答量:225
采纳率:100%
帮助的人:172万
展开全部
实际上不必用费劲的常数,模拟按键都是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也可以用这种方法
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
wu_kang_lin
2012-02-07 · TA获得超过170个赞
知道小有建树答主
回答量:490
采纳率:50%
帮助的人:314万
展开全部
TAB键与回车键都是按键.
既然能够发送TAB(VK_TAB)键.为何转个弯了想呢? 不会是复制的吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式