Option Explicit
[b]'---------- Mouse Wheel Declares -------- [Thank Hypetia][/b]
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public 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
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Const GWL_WNDPROC = (-4)
Public lpPrevWndProc As Long
Const WM_MOUSEWHEEL = &H20A
Const WHEEL_DELTA = 120
Dim Count As Integer
[b]'---------- Window Size Declares -------- [Thank strongm][/b]
' Some naughty globals
Public lpOldProc As Long
Public hWndSaved As Long
Public Hooked As Boolean
' Declarations for MINMAX stuff
Type POINTAPI
x As Long
y As Long
End Type
Type MINMAXINFO
ptReserved As POINTAPI
ptMaxSize As POINTAPI
ptMaxPosition As POINTAPI
ptMinTrackSize As POINTAPI
ptMaxTrackSize As POINTAPI
End Type
Public Const WM_GETMINMAXINFO = &H24
' Declarations required for our subclassing
'***** The Following Declared Above ******
'* Public Const GWL_WNDPROC = (-4)
'* Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
'* 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
'* ' RtlMoveMemory is here aliased as CopyMemory in line with Bruce McKinney, who revealed
'* ' the function in the original 'HardCore Basic'. This is one hell of a handy function
'* Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
[b]'---------- Mouse Wheel Functions -------- [Thank Hypetia][/b]
Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If Msg = WM_MOUSEWHEEL Then
Dim Delta As Long
Static Travel As Long
Delta = HiWord(wParam)
Travel = Travel + Delta
MouseWheel Travel \ WHEEL_DELTA, LoWord(lParam), HiWord(lParam)
Travel = Travel Mod WHEEL_DELTA
End If
WndProc = CallWindowProc(lpPrevWndProc, hWnd, Msg, wParam, lParam)
End Function
Sub MouseWheel(Travel As Integer, x As Long, y As Long)
Count = Count + Travel
Form1.Cls
Form1.Print "Travel=" & Count, "X=" & x, "Y=" & y
End Sub
Function HiWord(DWord As Long) As Integer
CopyMemory HiWord, ByVal VarPtr(DWord) + 2, 2
End Function
Function LoWord(DWord As Long) As Integer
CopyMemory LoWord, DWord, 2
End Function
[b]'---------- Window Size Functions -------- [Thank strongm][/b]
Public Function WindowFunction(ByVal hWnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim mmxMinMax As MINMAXINFO
' Is it the message we are interested in?
If iMsg = WM_GETMINMAXINFO Then
' The big problem here is that lParam is a pointer to a MINMAXINFO
' structure, and VB does not know how to deal with such a pointer.
' Since we need access to that structure to be able to make changes we
' we need some method of dereferencing the pointer properly.
' The following does exactly that:
CopyMemory mmxMinMax, ByVal lParam, Len(mmxMinMax)
' Change the min tracking size to something arbitary
mmxMinMax.ptMinTrackSize.x = 400
mmxMinMax.ptMinTrackSize.y = 400
' Now we need to make sure that the MINMAXINFO structure
' referenced by lParam contains our updated information.
' Copy the updated MINMAXINFO structure back to the
' over that referenced by lParam
CopyMemory ByVal lParam, mmxMinMax, LenB(mmxMinMax)
' Now call the default window procedure...
DefWindowProc hWnd, iMsg, wParam, lParam
' ...and then return 0& as we are supposed to if we deal with this message
WindowFunction = 0&
Exit Function
End If
' OK, let Windows handle all other messages
WindowFunction = CallWindowProc(lpOldProc, hWnd, iMsg, wParam, lParam)
End Function
Sub Unhook(hWnd As Long)
If Hooked Then
Call SetWindowLong(hWnd, GWL_WNDPROC, lpOldProc)
Hooked = False
End If
End Sub
Sub HookIt(hWnd As Long)
If Not Hooked Then
' Warning! Make sure that procedure we are hooking in has the
' exact same parameters as the one being hooked, else we can
' kiss the application goodbye
lpOldProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowFunction)
Hooked = True
End If
End Sub