全局热键

'窗体代码
Private Sub Form_Load()
    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



评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.