Private Declare Function RegisterHotKey Lib "User32" (ByVal hWnd As Long, ByVal ID As Long, _
ByVal fsModifiers As Long, ByVal vk As Long) As Long
Private Declare Function UnregisterHotKey Lib "User32" (ByVal hWnd As Long, ByVal ID As Long) As Long
Private Const MOD_CONTROL As Long = 2
Private Sub Command1_Click()
Me.Hide
End Sub
Private Sub Form_Load()
Dim lKeyFlag As Long
Call HOOK(hWnd)
' 注册热键并标志是否成功
lKeyFlag = RegisterHotKey(hWnd, 1, MOD_CONTROL, vbKeyF12) ' Ctrl + F12
lKeyFlag = lKeyFlag * 2 + RegisterHotKey(hWnd, 2, MOD_CONTROL, vbKeyU) ' Ctrl + U
If (lKeyFlag = 3) Then
Me.Visible = False
Else
MsgBox "注册热键失败!", 48
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call UnHOOK(hWnd)
Call UnregisterHotKey(hWnd, 1)
Call UnregisterHotKey(hWnd, 2)
End Sub
' 标准模块代码:
Option Explicit
Private Declare Function CallWindowProc Lib "User32" Alias "CallWindowProcW" ( _
ByVal lppreWinProc As Long, _
ByVal hWnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongW" ( _
ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongW" ( _
ByVal hWnd As Long, ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Private Const WM_HOTKEY As Long = &H312
Private Const GWL_WNDPROC As Long = -4
Private preWinProc As Long
'Private mlHookWnd As Long
Public Sub HOOK(hWnd As Long)
preWinProc = GetWindowLong(hWnd, GWL_WNDPROC)
Call SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub
Public Sub UnHOOK(hWnd As Long)
Call SetWindowLong(hWnd, GWL_WNDPROC, preWinProc)
End Sub
' * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
'热键拦截过程
Private Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
If (WM_HOTKEY = Msg) Then ' 热键消息
Call HotKeyDone(wParam)
End If
WndProc = CallWindowProc(preWinProc, hWnd, Msg, wParam, lParam)
End Function
Private Sub HotKeyDone(KeyID)
If (KeyID > 2) Then Exit Sub
If (KeyID = 1) Then
Form1.Visible = True
Else
MsgBox "执行功能2……", vbInformation
End If
End Sub