vb 如何向后台发送组合键ctrl+enter
|
admin
2014年4月15日 9:53
本文热度 6591
|
不要弹出目标窗口,就直接在后台发送ctrl+enter
首先,你必须获取到目标窗口的Hwnd,然后分二步:
'第一步,建立一个模块Module1.bas,复制下面代码到模块中,并保存
'===Module1.bas代码====
Option Explicit
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) 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 Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds 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 Const WM_SETFOCUS = &H7
Private Const KEYEVENTF_EXTENDEDKEY = &H1
Private Const KEYEVENTF_KEYUP = &H2
Public Sub KeyDown(ByVal vKey As Long) '用keybd_event模拟按下键盘
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY, 0
End Sub
Public Sub KeyUp(ByVal vKey As KeyCodeConstants) '用keybd_event模拟松开键盘
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub
Public Function SendKeyToWnd(MainWnd As Long, vbkey, Shift) As Boolean
SetForegroundWindow MainWnd'把目标窗口置前
SendMessage MainWnd, WM_SETFOCUS, 0&, 0& '选中按钮
If Shift Then KeyDown Shift '复合键
KeyDown vbkey '普通键
Sleep 100
KeyUp vbkey
If Shift Then KeyUp Shift
End Function
'第二步,函数调用:在你的工程内添加文件,选择刚才保存的文件Module1.bas,然后在调用处输入以下代码即可。
SendKeyToWnd hwnd, vbKeyZ, vbKeyMenu ‘向hwnd对应的窗口发送ALT+Z命令
该文章在 2014/4/15 9:53:25 编辑过