本帖最后由 作者 于 2005-8-12 22:15:23 编辑
1、新建一个 模块,加入以下代码:
Option Explicit '新建一个模块,粘贴这里代码。 Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _ ByVal hwnd As Long, _ ByVal nIndex As Long, _ ByVal dwNewLong As Long _ ) As Long Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( _ ByVal OldwndProc As Long, _ ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long _ ) As Long Public Const GWL_WNDPROC = -4 Public OldwndProc As Long Public Declare Function RegisterHotKey Lib "user32" ( _ ByVal hwnd As Long, _ ByVal HotKeyID As Long, _ ByVal fsModifiers As Long, _ ByVal vKey As Long _ ) As Long Public Declare Function UnregisterHotKey Lib "user32" ( _ ByVal hwnd As Long, _ ByVal HotKeyID As Long) As Long
Public HKeyID As Long '定义热键,需要注册多个热键可以使用数组
Public Function WindowProc( _ ByVal hwnd As Long, _ ByVal WindowMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As Long) As Long Select Case WindowMsg Case WM_HOTKEY Select Case wParam Case HKeyID MsgBox "在这里添加需要执行的代码或者函数" End Select End Select
WindowProc = CallWindowProc(OldwndProc, hwnd, WindowMsg, wParam, lParam)
End Function
2、然后在Form_Load中添加代码来注册热键:
Private Sub Form_Load() Dim ReturnValue As Long OldwndProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindowProc) '定义HOME键为热键 ReturnValue = RegisterHotKey(hwnd, HKeyID, 0, 36) End Sub
3、在Form_Unload中添加代码来卸载热键:
Private Sub Form_Unload(Cancel As Integer) Dim ReturnValue As Long ReturnValue = UnregisterHotKey(hwnd, HKeyID) End Sub
|