rt

解决方案 »

  1.   

    Dim WithEvents hk As clsRegHotKey
    Private Sub Form_Load()
         Me.Show
         Me.Hide
         Set hk = New clsRegHotKey
         hk.RegeditHotKeys Me.hwnd, CtrlKey, vbKeyS
         
    End SubPrivate Sub Form_Unload(Cancel As Integer)
        hk.UnRegeditHotKeys
        Set hk = Nothing
        
    End SubPrivate Sub hk_HotKeysDown()
        Me.Show
    End Sub
    '类名 clsRegHotKey
    Private Type POINTAPI
            x As Long
            y As Long
    End TypePrivate Type Msg
        hwnd As Long
        Message As Long
        wParam As Long
        lParam As Long
        time As Long
        pt As POINTAPI
    End TypePrivate Const PM_REMOVE = &H1
    Private Const WM_HOTKEY = &H312
    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
    '************************************************************
    Enum ShiftKeys
        AltKey = &H1
        CtrlKey = &H2
        ShiftKey = &H4
    End Enum
    Private bCancel As Boolean
    Private clshwnd
    Public Event HotKeysDown()
    '注册热键,等候按键消息
    Sub RegeditHotKeys(ByVal hwnd As Long, ByVal ShiftKey As ShiftKeys, ByVal ComKey As KeyCodeConstants)
        RegisterHotKey hwnd, &HBFFF&, ShiftKey, ComKey '注册热键
        clshwnd = hwnd
        bCancel = False
        Dim Message As Msg
        Do While Not bCancel
            
            WaitMessage '等候按键消息
            '判断消息
            If PeekMessage(Message, hwnd, WM_HOTKEY, WM_HOTKEY, PM_REMOVE) Then
                RaiseEvent HotKeysDown '引发事件
            End If
            DoEvents
        Loop
    End Sub
    '取消热键注册
    Sub UnRegeditHotKeys()
        bCancel = True
        UnregisterHotKey clshwnd, &HBFFF&
    End Sub