Results 1 to 7 of 7

Thread: Bloody Mousheel - Nearly done, still not perfect. Please help!

  1. #1

    Thread Starter
    Lively Member ice & fire's Avatar
    Join Date
    Oct 2001
    Location
    Land of ice & fire
    Posts
    110

    Bloody Mousheel - Nearly done, still not perfect. Please help!

    Hi folks!
    Sorry for bothering you all the time with that bloody Mouswheel:

    I have that Code in a module:
    VB Code:
    1. Option Explicit
    2.  
    3. 'API Constants
    4. Public Const WH_MOUSE As Long = 7
    5.  
    6. 'API Declarations
    7. Public 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
    8. Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    9. Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    10.  
    11. 'Global mouse function callback handles
    12. Public g_hMouseHook As Long
    13. Public Const WM_MOUSEWHEEL = &H20A
    14.  
    15. Public Function MouseProc(ByVal idHook As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    16.     'Mouse Function Hook...
    17.     If idHook < 0 Then
    18.         MouseProc = CallNextHookEx(g_hMouseHook, idHook, wParam, ByVal lParam)
    19.     Else
    20.         If wParam = WM_MOUSEWHEEL Then
    21.             Debug.Print idHook & ", " & wParam & ", " & lParam
    22.         Else
    23.             MouseProc = CallNextHookEx(g_hMouseHook, idHook, wParam, ByVal lParam)
    24.         End If
    25.     End If
    26. End Function
    27.  
    28. Public Sub HookMouse()
    29.     If g_hMouseHook = 0 Then g_hMouseHook = SetWindowsHookEx(WH_MOUSE, AddressOf MouseProc, App.hInstance, App.ThreadID)
    30. End Sub
    31.  
    32. Public Sub UnhookMouse()
    33.     If g_hMouseHook Then
    34.         Call UnhookWindowsHookEx(g_hMouseHook)
    35.         g_hMouseHook = 0
    36.     End If
    37. End Sub

    And this one in a Form

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. HookMouse
    5. End Sub
    6.  
    7. Private Sub Command2_Click()
    8. UnhookMouse
    9. End Sub
    10.  
    11.  
    12. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    13. UnhookMouse
    14. End Sub

    It tells me, if someone uses the MouseWheel, but not in which direction (UP, DOWN)

    Please help....

  2. #2
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    well, if you want to give up on the code...

    there's a free mousewheel component to download...
    mousewheel

  3. #3
    RoyceWindsor1
    Guest
    Using this example, wparam is negative when the wheel is rolling towards the user.

    From MSDN:

    Code:
    Option Explicit
    
    Private MSWHEEL_ROLLMSG     As Long
    Private m_PrevWndProc       As Long
    Private Const GWL_WNDPROC = (-4)
    
    Private 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
    Private Declare Function SetWindowLong Lib "user32" Alias _
       "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, _
       ByVal dwNewLong As Long) As Long
    Private Declare Function RegisterWindowMessage Lib "user32" _
       Alias "RegisterWindowMessageA" (ByVal lpString As String) As Long
    
    Public Sub SubClassHookForm()
       MSWHEEL_ROLLMSG = RegisterWindowMessage("MSWHEEL_ROLLMSG")
       ' On Windows NT 4.0, Windows 98, and Windows Me, change the above line to
       ' MSWHEEL_ROLLMSG = &H20A
       m_PrevWndProc = SetWindowLong(Form1.hwnd, GWL_WNDPROC, _
                                     AddressOf WindowProc)
    End Sub
    Public Sub SubClassUnHookForm()
       Call SetWindowLong(Form1.hwnd, GWL_WNDPROC, m_PrevWndProc)
    End Sub
    
    Public Function WindowProc(ByVal hwnd As Long, ByVal msg As Long, _
       ByVal wParam As Long, ByVal lParam As Long) As Long
    
       If msg = MSWHEEL_ROLLMSG Then
          Debug.Print "Receive MSWHEEL_ROLLMSG"
       End If
       WindowProc = CallWindowProc(m_PrevWndProc, hwnd, msg, wParam, lParam)
    End Function
    John

  4. #4

    Thread Starter
    Lively Member ice & fire's Avatar
    Join Date
    Oct 2001
    Location
    Land of ice & fire
    Posts
    110
    Duh, it doesn´t work!
    It says: Wrong use of AdressOf Operator!

  5. #5

    Thread Starter
    Lively Member ice & fire's Avatar
    Join Date
    Oct 2001
    Location
    Land of ice & fire
    Posts
    110
    Sorry, im silly!
    It does work!

    Thanks man!!!

  6. #6
    RoyceWindsor1
    Guest
    Duh, put it in a module.

    You can't use AddressOf on a proc in a form.

    Put this in a form:

    Code:
    Option Explicit
    
    Private Sub Form_Load()
       Me.Show
       Call SubClassHookForm
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
       Call SubClassUnHookForm
    End Sub
    ----

    No problem

  7. #7

    Thread Starter
    Lively Member ice & fire's Avatar
    Join Date
    Oct 2001
    Location
    Land of ice & fire
    Posts
    110
    Thanks, as i said, i forgot the AdressOf Procedure Crap.
    It struck me two seconds after posting....

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