關於部落格
順子&阿芳的部落格
  • 86677

    累積人氣

  • 1

    今日人氣

    2

    追蹤人氣

『VB』使用 WH_MOUSE_LL 攔截滑鼠訊號

Option Explicit
Declare Function SetWindowsHookEx
Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Public hNextHookProc As Long
Private Const HC_ACTION As Integer = 0
Private Const WH_MOUSE As Integer = 7
Private Const WH_MOUSE_LL As Integer = 14
Private Const WM_MOUSEMOVE As Integer = &H200
Private Const WM_LBUTTONDOWN As Integer = &H201
Private Const WM_LBUTTONUP As Integer = &H202
Private Const WM_LBUTTONDBLCLK As Integer = &H203
Private Const WM_RBUTTONDOWN As Integer = &H204
Private Const WM_RBUTTONUP As Integer = &H205
Private Const WM_RBUTTONDBLCLK As Integer = &H206
Private Const WM_MBUTTONDOWN As Integer = &H207
Private Const WM_MBUTTONUP As Integer = &H208
Private Const WM_MBUTTONDBLCLK As Integer = &H209
Private Const WM_MOUSEWHEEL As Integer = &H20A

Public nX, nY As Long
Public dX, dY As Long
Public XLen, YLen As Long
Public IsMove, MoveState As Boolean

Public Type POINTAPI
    x As Long
    y As Long
End Type

Public Type MSLLHOOKSTRUCT
    pt As POINTAPI
    mouseData As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type


Public Sub UnHookKBD()
    If hNextHookProc <> 0 Then
        UnhookWindowsHookEx hNextHookProc
        hNextHookProc = 0
    End If
End Sub
Public Function EnableKBDHook()
    If hNextHookProc <> 0 Then
        Exit Function
    End If
    hNextHookProc = SetWindowsHookEx(WH_MOUSE_LL, AddressOf _
    MyKBHFunc, App.hInstance, 0)
    If hNextHookProc <> 0 Then
        EnableKBDHook = hNextHookProc
    End If
End Function

Public Function MyKBHFunc(ByVal iCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    '這三個參數是固定的不能動,而 MyKBHFunc 這個名稱只要和
 
   'SetWindowsHookex()中 AddressOf 後的名稱一樣就可以
    'wParam 是傳入按了那個鍵的 Virtual-Key code

    '如果將下列兩行的 unmark ,所有輸入的鍵盤訊號都會無效
    'MyKBHFunc = 1 吃掉訊息
    'Exit Function   
    Dim pmshs As MSLLHOOKSTRUCT
    Dim i As Integer
   
    If iCode = HC_ACTION Then
        Select Case wParam
            Case WM_MOUSEMOVE
                CopyMemory pmshs, ByVal lParam, Len(pmshs)
        End Select
    End If

    Call CallNextHookEx(hNextHookProc, iCode, wParam, lParam)
End Function

相簿設定
標籤設定
相簿狀態