VB模拟按键 针对固定的窗口生效

来源:百度知道 编辑:UC知道 时间:2024/07/08 02:08:27
我制作出来的VB模拟按键都是针对当前窗口生效
能不能设计个方案固定的对一个窗口一直生效呢?
尽量给点程序!
请高手赐教!
比如只针对于程序123.exe生效,不管他是不是当前窗口!

Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private wnd As Long

Private Sub Form_Load()
Dim wnd As Long, A As String
wnd = FindWindow(vbNullString, "无标题 - Notepad2") '获取窗口句柄在这里修改想要窗口标题,我是用记事本测试的
If wnd = 0 Then
MsgBox "该窗口未运行", 16, "Error"
Unload Me
Exit Sub
End If
Do
Sleep 30000 '隔30秒
PostMessage wnd, &H100, vbKeyF9, 0 '发送按键f9
Sleep 10000 '隔10秒
PostMessage wnd, &H100, vbKeyF3, 0 '发送按键f3
Loop '返回do继续循环

End Sub

注意:只能按下单个按键