不要彈出目標(biāo)窗口,就直接在后臺(tái)發(fā)送ctrl+enter
首先,你必須獲取到目標(biāo)窗口的Hwnd,然后分二步:
'第一步,建立一個(gè)模塊Module1.bas,復(fù)制下面代碼到模塊中,并保存
'===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模擬松開(kāi)鍵盤
keybd_event vKey, 0, KEYEVENTF_EXTENDEDKEY Or KEYEVENTF_KEYUP, 0
End Sub
Public Function SendKeyToWnd(MainWnd As Long, vbkey, Shift) As Boolean
SetForegroundWindow MainWnd'把目標(biāo)窗口置前
SendMessage MainWnd, WM_SETFOCUS, 0&, 0& '選中按鈕
If Shift Then KeyDown Shift '復(fù)合鍵
KeyDown vbkey '普通鍵
Sleep 100
KeyUp vbkey
If Shift Then KeyUp Shift
End Function
'第二步,函數(shù)調(diào)用:在你的工程內(nèi)添加文件,選擇剛才保存的文件Module1.bas,然后在調(diào)用處輸入以下代碼即可。
SendKeyToWnd hwnd, vbKeyZ, vbKeyMenu ‘向hwnd對(duì)應(yīng)的窗口發(fā)送ALT+Z命令
該文章在 2014/4/15 9:53:25 編輯過(guò)