How do I capture the movement of the mouse wheel?
When the user tracks up or tracks down with the mouse wheel, I would like to be able to have my program react to that movement. How would I go about doing that?
Process the WM_MOUSEWHEEL message.
http://msdn.microsoft.com/en-us/library/ms645617%28VS.85%29.aspx
One thing to watch for with WM_MOUSEWHEEL is that the delta value is handled differently on older versions of Windows. Up through Windows XP, the scroll delta was typically calculated as HIWORD(wParam) / WHEEL_DELTA, where WHEEL_DELTA was 120. In other words, the values that were passed in were always a multiple of 120. With the release of Vista, this changed so that it's possible for the delta value to be less than WHEEL_DELTA so the recommendation is that you should accumulate the values until WHEEL_DELTA is reached.
Also, just so you're aware, although wParam is typically specified as an unsigned integer, the actual delta value can be positive or negative. In C/C++, the delta value is typically determined something like: nDelta = (short)HIWORD(wParam). In other words, if the high bit of the high word for wParam is set, then the user is scrolling the mouse wheel backwards; otherwise they're scrolling fowards. Some applications goof on this, treating the delta value as an unsigned integer, and the end result is that no matter which way you scroll the mouse wheel, the program scrolls forward.
Thanks Jose' and Mike for the info.
I am having a difficult time understanding.
Can someone complete this function?
Function WheelMovement
Local UpMove As Long
Local DnMove As Long
Local tVal As Long
UpMove = ???? Mouse Upward movement
DnMove = ???? Mouse Downward movement
If DnMove <> 0 Then tVal = tVal - 1
If UpMove <> 0 Then tVal = tVal + 1
FF_TextBox_SetText( HWND_FORM1_Text1, Str$(tVal))
DnMove = 0
UpMove = 0
End Function
Here is how I use it. This code is directly from the JellyEdit.dll code editor that I wrote for use in FF3. To use in FF, the best bet would be for you to deal with the %WM_MOUSEWHEEL message through your CUSTOM message handler.
'//
'// WM_MOUSEWHEEL handler
'//
Function JellyEdit_OnMouseWheel( ByVal hWnd As Dword, _
ByVal wParam As Dword, _
ByVal lParam As Long _
) As Long
Local nDelta As Integer ' this must be integer, not long.
Local nScrollCount As Long
Local y As Long
nDelta = HiWrd(wParam)
nScrollCount = 3
get_ed_macro
If (LoWrd(wParam) And %MK_MBUTTON) = %MK_MBUTTON Then
' horizontal scroll
SystemParametersInfo %SPI_GETWHEELSCROLLCHARS, 0, nScrollCount, 0
For y = 1 To nScrollCount
SendMessage hWnd, %WM_HSCROLL, _
Mak(Long, IIf&(nDelta > 1, %SB_LINELEFT, %SB_LINERIGHT), 0), _
0
Next
Else
' vertical scroll
SystemParametersInfo %SPI_GETWHEELSCROLLLINES, 0, nScrollCount, 0
For y = 1 To nScrollCount
SendMessage hWnd, %WM_VSCROLL, _
Mak(Long, IIf&(nDelta > 1, %SB_LINEUP, %SB_LINEDOWN), 0), _
0
Next
End If
End Function
Thanks Paul. I will see if I can get a demo program working. When I do I will post it here so others needing help can examine it.
Paul, I didn't need to use your code to capture MouseWheel movement. I think this method works better for my purposes. But thanks for your help.
Ok, I have a demo program working that show how to capture the MouseWheel movement. The demo simply increase or decreases a Value in a TextBox as the mouse wheel is moved up or down. The procedure is this simple code that I added to the FF_PumpHook function.
Select Case GetParent(Msg.hWnd)
Case HWND_FORM1
Select Case Msg.Message
Case %WM_MOUSEWHEEL
If Msg.wParam = -7864320 Then
' Increase value in TextBox when MouseWheel moves up
tVal = tVal + 1
FF_TextBox_SetText( HWND_FORM1_Text1, Str$(tVal))
End If
If Msg.wParam = 7864320 Then
' Decrease value in TextBox when MouseWheel moves down
tVal = tVal - 1
FF_TextBox_SetText( HWND_FORM1_Text1, Str$(tVal))
End If
End Select
End Select
I have attached the demo program for anyone interested. Note: I used literals instead of the windows system constant, simply because I didn't know the name for the MouseWheel down movement constant nor the up movement constant. If someone knows the names for those constants, please share.
My intended use for this procedure is to navigate a database using the MouseWheel. The ability to capture the MouseWheel movement may open up other posibilities as well.