Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programming for the mouse wheel 9

Status
Not open for further replies.

elHollandes

Programmer
Feb 22, 2004
12
0
0
ES
How do I make my vb6 program react on the mouse wheel...
Have been searching for this before and it used to be complicated, but by now maybe someone has a solition.

 
Windows sends WM_MOUSEWHEEL notification to the active window when the mouse wheel is rotated. You can subclass a window to intercept this message and respond to this event.

See the following example.
The following code goes in your form.
___
[tt]
Private Sub Form_Load()
'subclass the window
lpPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WndProc)
End Sub[/tt]
___

Now add a module to your project and insert the following code.
___
[tt]
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
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[/tt]
___

Run your program and scroll your wheel over Form1. The rotation count along with mouse position will be displayed on the form.

You will notice that the WM_MOUSEWHEEL message is handled in a little odd way, although it can be done in a simpler manner. The reason for this is explained in the remarks section of the WM_MOUSEWHEEL reference.
 
Thank you so much for this valuable message! I have spent a whole day in google trying to find how to get wheel information in VB applications and finally got the answer here! Thank you so much!!!!!!!!

----Sean from Rockford
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top