请问如何注册热键?例如按下ctrl+art+F5 就呼出窗口,或者隐藏窗口,或者执行一段程序。
还有怎样检测要注册的热键是否已被占用?谢谢各位。
还有怎样检测要注册的热键是否已被占用?谢谢各位。
解决方案 »
- 跪求在EXCEL2007中利用VBA读取XML文件中指定元素,并将结果保存到CSV文件中。
- 如何让EXE程序在运行时,删除这个文件本身?我有一个可行的办法.(纯讨论,所以分不多.)
- 用WINSOCK|INNET|XMLHTTP获取远程网页哪个速度最快?
- 请帮我看一下,这些代码有什么错
- 求代码,关于运用Timer强制中止功能的问题
- 做过串口通讯的人请进来!
- vb如何控制文件夹的删除、移动、复制。
- 一个关于字符串的问题!请各位都来看看!
- 请问各位大侠哪里有下载msxml4及其以上版本的
- 怎样获取SCSI硬盘的序列号(ide硬盘序列号已能获取)
- 咨询这个软件的源代码值几多钱?[顶者都有分]
- 选用哪个XP风格的控件,让窗体更漂亮呢
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 GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal lpString As String) As Integer
Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal nAtom As Integer) As Integer
Private Declare Function UnregisterHotKey Lib "user32" (ByVal hwnd As Long, ByVal id As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As LongConst GWL_WNDPROC = (-4)
Const WM_HOTKEY = &H312Public Enum ModKeys
MOD_ALT = &H1
MOD_CONTROL = &H2
MOD_SHIFT = &H4
MOD_WIN = &H8
End EnumDim iAtom As Integer
Dim OldProc As Long, hOwner As LongPublic Function SetHotKey(hWin As Long, ModKey As ModKeys, vKey As Long) As Boolean
If hOwner > 0 Then Exit Function
hOwner = hWin
iAtom = GlobalAddAtom("MyHotKey")
SetHotKey = RegisterHotKey(hOwner, iAtom, ModKey, vKey)
OldProc = SetWindowLong(hOwner, GWL_WNDPROC, AddressOf WndProc)
End FunctionPublic Sub RemoveHotKey()
If hOwner = 0 Then Exit Sub
Call UnregisterHotKey(hOwner, iAtom)
Call SetWindowLong(hOwner, GWL_WNDPROC, OldProc)
End SubPublic Function WndProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_HOTKEY And wParam = iAtom Then
'你要执行的代码
Else
WndProc = CallWindowProc(OldProc, hwnd, wMsg, wParam, lParam)
End If
End Function
设置热键:SetHotKey Me.hwnd, MOD_CONTROL + MOD_ALT, vbKeyF5
取消热键:RemoveHotKey
Private Const MOD_CONTROL = &H2
Private Const MOD_SHIFT = &H4
Private Const PM_REMOVE = &H1
Private Const WM_HOTKEY = &H312
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type Msg
hWnd As Long
Message As Long
wParam As Long
lParam As Long
time As Long
pt As POINTAPI
End Type
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 Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As Msg, ByVal hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal wRemoveMsg As Long) As Long
Private Declare Function WaitMessage Lib "user32" () As Long
Private bCancel As Boolean
Private Sub ProcessMessages()
Dim Message As Msg
'loop until bCancel is set to True
Do While Not bCancel
'wait for a message
WaitMessage
'check if it's a HOTKEY-message
If PeekMessage(Message, Me.hWnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
'minimize the form
WindowState = vbMinimized
End If
'let the operating system process other events
DoEvents
Loop
End Sub
Private Sub Form_Load()
Dim ret As Long
bCancel = False
'register the Ctrl-F hotkey
ret = RegisterHotKey(Me.hWnd, &HBFFF&, MOD_CONTROL, vbKeyF)
'show some information
Me.AutoRedraw = True
Me.Print "Press CTRL-F to minimize this form"
'show the form and
Show
'process the Hotkey messages
ProcessMessages
End Sub
Private Sub Form_Unload(Cancel As Integer)
bCancel = True
'unregister hotkey
Call UnregisterHotKey(Me.hWnd, &HBFFF&)
End Sub