Results 1 to 2 of 2

Thread: Mouse Wheel Trouble

  1. #1

    Thread Starter
    Addicted Member -SUBS-Lenos's Avatar
    Join Date
    Sep 2005
    Posts
    179

    Mouse Wheel Trouble

    Hi!

    For some reason vb crashes every time I use This code

    Quote Originally Posted by manavo11
    Subclassing :

    VB Code:
    1. '**************
    2. '* Form Code
    3. '**************
    4. Option Explicit
    5.  
    6. Private Sub Form_Load()
    7.     Hook Me.hwnd
    8. End Sub
    9.  
    10. Private Sub Form_Unload(Cancel As Integer)
    11.     Unhook Me.hwnd
    12. End Sub

    VB Code:
    1. '**************
    2. '* Standard Module Code
    3. '**************
    4. Option Explicit
    5.  
    6. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, _
    7.                                                                             ByVal nIndex As Long, _
    8.                                                                             ByVal dwNewLong As Long) _
    9.                                                                             As Long
    10. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
    11.                                                                               ByVal hwnd As Long, _
    12.                                                                               ByVal Msg As Long, _
    13.                                                                               ByVal wParam As Long, _
    14.                                                                               ByVal lParam As Long) _
    15.                                                                               As Long
    16. Private Declare Function DefWindowProc Lib "user32" Alias "DefWindowProcA" (ByVal hwnd As Long, _
    17.                                                                             ByVal wMsg As Long, _
    18.                                                                             ByVal wParam As Long, _
    19.                                                                             ByVal lParam As Long) _
    20.                                                                             As Long
    21.  
    22. Private Const GWL_WNDPROC = (-4)
    23.  
    24. Private Const WM_MOUSEWHEEL = &H20A
    25.  
    26. Private mPrevProc As Long
    27.  
    28. Public Sub Hook(ByVal hwnd As Long)
    29.     mPrevProc = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WndProc)
    30. End Sub
    31.  
    32. Public Sub Unhook(ByVal hwnd As Long)
    33.  
    34.     Call SetWindowLong(hwnd, GWL_WNDPROC, mPrevProc)
    35.     mPrevProc = 0&
    36.  
    37. End Sub
    38.  
    39. Public Function WndProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    40. On Error Resume Next
    41.    
    42. Dim x As Long, y As Long, delta As Long
    43.    
    44.     If uMsg = WM_MOUSEWHEEL Then
    45.         delta = HiWord(wParam)
    46.        
    47.         x = LoWord(lParam)
    48.         y = HiWord(lParam)
    49.        
    50.         Debug.Print "Scrolling " & IIf((delta > 0), "Up", "Down") & " ,X:" & x & ",Y:" & y
    51.     End If
    52.  
    53.     If mPrevProc& Then
    54.         WndProc = CallWindowProc(mPrevProc, hwnd, uMsg, wParam, lParam)
    55.     Else
    56.         WndProc = DefWindowProc(hwnd, uMsg, wParam, lParam)
    57.     End If
    58.    
    59. End Function
    60.  
    61. Private Function LoWord(DWord As Long) As Long
    62.     If DWord And &H8000& Then
    63.         LoWord = DWord Or &HFFFF0000
    64.     Else
    65.         LoWord = DWord And &HFFFF&
    66.     End If
    67. End Function
    68.  
    69. Private Function HiWord(DWord As Long) As Long
    70.     HiWord = (DWord And &HFFFF0000) \ &H10000
    71. End Function
    Is there any simple code that works to detect if the mouse is being scrolled up or down?

    Thanks

  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Mouse Wheel Trouble

    works for me.. no crash but....
    can I take a guess that IDE crashes when u hit stop??

    when subclassing.. u cant hit stop. u MUST click the X on the form.. (or use code to close it.. like behind a cmdButton..) Subclassing will crash the IDE if not UnHooked
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width