Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type MSLLHOOKSTRUCT
pt As POINTAPI
mousedata As Long
flags As Long
time As Long
dwExtraInfo As Long
End Type
Private Const HC_ACTION = 0
Private Const WH_MOUSE_LL = 14
Private Const WM_MOUSEWHEEL = &H20A
Private Const GWL_HINSTANCE = (-6)
Private oObject As Object
Private bHooked As Boolean
#If VBA7 Then
Declare PtrSafe Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As LongPtr, ByVal hmod As LongPtr, ByVal dwThreadId As Long) As LongPtr
Declare PtrSafe Function CallNextHookEx Lib "user32" (ByVal hHook As LongPtr, ByVal ncode As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
Declare PtrSafe Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As LongPtr) As Long
Private lLowLevelMouse As LongPtr
#Else
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 CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hhk As Long) As Long
Private lLowLevelMouse As Long
#End If
'====================='
'\\ Public Routines '
'====================='
Public Property Let MakeScrollableWithMouseWheel(ByVal Obj As Object, ByVal vNewValue As Boolean)
If vNewValue Then
Hook_Mouse
Else
UnHook_Mouse
End If
Set oObject = Obj
bHooked = vNewValue
End Property
Public Property Get MakeScrollableWithMouseWheel(ByVal Obj As Object) As Boolean
MakeScrollableWithMouseWheel = bHooked
End Property
'====================='
'\\ Private Routines '
'====================='
#If VBA7 Then
Private Function LowLevelMouseProc(ByVal ncode As Long, ByVal wParam As LongPtr, lParam As MSLLHOOKSTRUCT) As LongPtr
#Else
Private Function LowLevelMouseProc(ByVal ncode As Long, ByVal wParam As Long, lParam As MSLLHOOKSTRUCT) As Long
#End If
On Error Resume Next
If (ncode = HC_ACTION) Then
If wParam = WM_MOUSEWHEEL Then
With oObject
If lParam.mousedata > 0 Then
.TopIndex = .TopIndex - 1
Else
.TopIndex = .TopIndex + 1
End If
End With
LowLevelMouseProc = -1
Exit Function
End If
End If
LowLevelMouseProc = CallNextHookEx(lLowLevelMouse, ncode, wParam, ByVal lParam)
End Function
Private Sub Hook_Mouse()
If lLowLevelMouse = 0 Then
#If VBA7 Then
lLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, AddressOf LowLevelMouseProc, Application.HinstancePtr, 0)
#Else
lLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, AddressOf LowLevelMouseProc, Application.Hinstance, 0)
#End If
End If
End Sub
Private Sub UnHook_Mouse()
If lLowLevelMouse <> 0 Then UnhookWindowsHookEx lLowLevelMouse: lLowLevelMouse = 0
End Sub