这个是占很少资源的 全局热键。
-----------在窗体-------------------
程序代码: | [ 复制代码 ] |
Private Sub Form_Load()
RegHotKey Me.hwnd, CTRL + ALT, vbKeyD
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnRegHotKey Me.hwnd
End Sub
RegHotKey Me.hwnd, CTRL + ALT, vbKeyD
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnRegHotKey Me.hwnd
End Sub
--------------在模块----------------
程序代码: | [ 复制代码 ] |
Option Explicit
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 Long
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 preWinProc As Long
Private Modifiers As Long, uVirtKey As Long, idHotKey As Long
Private Const WM_HOTKEY = &H312
Private Const GWL_WNDPROC = (-4)
Public Enum ThreeKey
CTRL = &H2
SHIFT = &H4
ALT = &H1
NONE = &H0
End Enum
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hWord As Integer
End Type
Private Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then '最好自己写
MsgBox "你按下了热键哦~"
End If
End If
End If
Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
Public Sub RegHotKey(FormHwnd As Long, fiers As ThreeKey, vKey As Long, Optional HotKey As Long = 1)
preWinProc = GetWindowLong(FormHwnd, GWL_WNDPROC)
SetWindowLong FormHwnd, GWL_WNDPROC, AddressOf Wndproc
idHotKey = HotKey
Modifiers = fiers
uVirtKey = vKey
RegisterHotKey FormHwnd, idHotKey, Modifiers, uVirtKey
End Sub
Public Sub UnRegHotKey(FormHwnd As Long)
SetWindowLong FormHwnd, GWL_WNDPROC, preWinProc
Call UnregisterHotKey(FormHwnd, uVirtKey)
End Sub
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 Long
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 preWinProc As Long
Private Modifiers As Long, uVirtKey As Long, idHotKey As Long
Private Const WM_HOTKEY = &H312
Private Const GWL_WNDPROC = (-4)
Public Enum ThreeKey
CTRL = &H2
SHIFT = &H4
ALT = &H1
NONE = &H0
End Enum
Private Type taLong
ll As Long
End Type
Private Type t2Int
lWord As Integer
hWord As Integer
End Type
Private Function Wndproc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_HOTKEY Then
If wParam = idHotKey Then
Dim lp As taLong, i2 As t2Int
lp.ll = lParam
LSet i2 = lp
If (i2.lWord = Modifiers) And i2.hWord = uVirtKey Then '最好自己写
MsgBox "你按下了热键哦~"
End If
End If
End If
Wndproc = CallWindowProc(preWinProc, hwnd, Msg, wParam, lParam)
End Function
Public Sub RegHotKey(FormHwnd As Long, fiers As ThreeKey, vKey As Long, Optional HotKey As Long = 1)
preWinProc = GetWindowLong(FormHwnd, GWL_WNDPROC)
SetWindowLong FormHwnd, GWL_WNDPROC, AddressOf Wndproc
idHotKey = HotKey
Modifiers = fiers
uVirtKey = vKey
RegisterHotKey FormHwnd, idHotKey, Modifiers, uVirtKey
End Sub
Public Sub UnRegHotKey(FormHwnd As Long)
SetWindowLong FormHwnd, GWL_WNDPROC, preWinProc
Call UnregisterHotKey(FormHwnd, uVirtKey)
End Sub
[本日志由 admin 于 2013-11-01 10:56 PM 编辑]
引用通告地址 (0):
http://www.cnng.net/blog/trackback.asp?tbID=52
http://www.cnng.net/blog/trackback.asp?tbID=52&CP=GBK
http://www.cnng.net/blog/trackback.asp?tbID=52
http://www.cnng.net/blog/trackback.asp?tbID=52&CP=GBK