如何为你的应用程序设置热键?
启动工程后,在你的工程中添加一个标准模块,然后将下面的代码粘贴到模块的声明段中。
Option Explicit
Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Long) As Long
Declare Function DefWindowProc Lib "user32" _
Alias "DefWindowProcA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Const WM_SETHOTKEY = &H32
Public Const WM_SHOWWINDOW = &H18
Public Const HK_SHIFTA = &H141 ''Shift + A
Public Const HK_SHIFTB = &H142 ''Shift * B
Public Const HK_CONTROLA = &H241 ''Control + A
Public Const HK_ALTZ = &H45A
请注意组合键的值必须以低/高位字节的格式进行声明。也就是说是一个十六进制的数字。后两位是低端字节,如41=a;前两位是高端字节,如01=1=shift。
在窗体的Load事件中加入下面的代码:
Private Sub Form_Load()
Dim erg As Long
Me.WindowState = vbMinimized
''让windows知道你想要的热键。
erg& = SendMessage(Me.hwnd, WM_SETHOTKEY,HK_ALTZ, 0)
''检查函数是否执行成功
If erg& <> 1 Then
MsgBox "You need another hotkey", vbOKOnly,"Error"
End If
''告诉windows热键按下后做什么--显示窗口
erg& = DefWindowProc(Me.hwnd, WM_SHOWWINDOW,0, 0)
End Sub
按F5运行程序后,再按下ALT+Z就会看到结果了.