VB sendkey可以向指定窗口发送消息吗?如果可以,怎么实现?

如题所述

可以的,比如先启动计算器,再在VB中运行下面的代码即可向计算器中输入数字:
AppActivate
"计算器"
SendKeys
"1234"
不过一般来说这个只能实现简单的功能,如果要想实现复杂一些的操作(比如向处于后台运行状态的窗口发送按键消息,并且不影响前台的键盘操作),就要借助API函数了,而且代码量也比较大。
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2019-10-23
Private Sub Form_Load()
Dim x
x = Shell("Notepad.exe", vbNormalFocus)
SendKeys "这是一个文本文件的测试程序.", True ' 模拟输入英文字符串
SendKeys "{Enter}{Enter}", True ' 两次回车
waittime (2) ' 延时 2 秒
SendKeys "自动输入文字", True
SendKeys "{Enter}{Enter}", True
waittime (2)
SendKeys "既不是病毒,也不是木马!", True
SendKeys "{Enter}{Enter}", True
waittime (2)
SendKeys "开始退出... 看菜单项{(}F{)}...", True
SendKeys "%", True 'Alt 键激活菜单条
waittime (1)
SendKeys "(F)", True '按字母 "F",选择"文件(F)"菜单
waittime (1)
SendKeys "(X)", True '按字母 "X", 选择"退出(X)"
waittime (2)
SendKeys "(N)", True '弹出一个对话框,询问文件是否存盘,按 "N" 不存盘
End '结束本模拟程序
End Sub
Private Sub waittime(delay As Single)
Dim starttime As Single
starttime = Timer
Do Until (Timer - starttime) > delay
Loop
End Sub本回答被网友采纳
第2个回答  2018-12-01
这个主意不错,分享了
相似回答