locked
Detecting a mouse move RRS feed

  • Question

  • Hi,

     

    I need to know the status of the mouse whether it is moving or being idle. How can I do that? 

    I have tried with form's mouse move event but, it doesn't work if the mouse is on any control.

     

    Thanks,

     

    Regards,

     

    Bobby


    Ayyagari
    Thursday, February 2, 2012 2:49 AM

Answers

  • Start a new WF app and replace the code on Form1 with:

    Public Class Form1
      
    Dim MouseMoveFilter As New FilterMouseMove
      
    Protected Overrides Sub OnLoad(e As EventArgs)
        
    MyBase.OnLoad(e)
        
    Application.AddMessageFilter(MouseMoveFilter)
        
    AddHandler MouseMoveFilter.MouseMoving, AddressOf MouseMoving
        MouseMoveFilter.Tmr.Interval = 1000
        MouseMoveFilter.Tmr.Start()
      
    End Sub
      
    Sub MouseMoving(Moving As Boolean, Ctrl As String)
        
    Me.Text = If(Moving, "Moving over " + Ctrl, "Not Moving")
      
    End Sub
      
    Class FilterMouseMove
        
    Implements IMessageFilter
        
    Friend Event MouseMoving(Moving As Boolean, ControlName As String)
        
    Friend WithEvents Tmr As New Timer
        
    Public Function PreFilterMessage(ByRef m As MessageAs Boolean _
                                         
    Implements IMessageFilter.PreFilterMessage
          
    If m.Msg = 512 Then
            
    RaiseEvent MouseMoving(TrueControl.FromHandle(m.HWnd).Name)
            Tmr.Start()
          
    End If
          
    Return False
        
    End Function
        
    Private Sub Tmr_Tick(sender As Object, e As EventArgsHandles Tmr.Tick
          Tmr.Stop()
          
    RaiseEvent MouseMoving(False"")
        
    End Sub
      
    End Class
    End Class


    Add some controls and move the cursor over the controls.


    • Edited by JohnWein Thursday, February 2, 2012 8:46 AM
    • Marked as answer by bbby A programmer Thursday, February 2, 2012 9:11 AM
    Thursday, February 2, 2012 8:16 AM

All replies

  • This is mostly done by saving the last point of the mouse and check if that is the same with the previous mouse down 

    You see it done on this page on our website

    http://www.vb-tips.com/MoveControl.aspx


    Success
    Cor
    Thursday, February 2, 2012 7:11 AM
  • You can create a sinlge mousemove event handler and tie each of the mouse move events from your controls to the one handler event.  This can be done by nominating each control, or by iterating through the form's control colelction.

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    AddHandler Me.MouseMove, AddressOf All_MouseMove
            
    AddHandler Button1.MouseMove, AddressOf All_MouseMove
            
    AddHandler ListBox1.MouseMove, AddressOf All_MouseMove
            
    AddHandler Label1.MouseMove, AddressOf All_MouseMove
        
    End Sub

        Private Sub All_MouseMove(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs)
            
    Dim C As Control = CType(sender, Control)
            
    Dim P As Point = C.PointToScreen(e.Location)
            Label1.Text = 
    Me.PointToClient(P).ToString
        
    End Sub

    To check whether or not the mouse has moved in a certain time period, use a timer.  Create a flag (a boolean variable).  Each time the mouse moves, set the flag to True to indicate 'there has been movement' .  Each time the timer ticks, check the state of the flag.  If it's false, the mouse hasn't moved since the last timer tick.  If it's true (there has been movement), set the flag to false, ready to be tested in the next timer tick.

    Thursday, February 2, 2012 7:57 AM
  • Start a new WF app and replace the code on Form1 with:

    Public Class Form1
      
    Dim MouseMoveFilter As New FilterMouseMove
      
    Protected Overrides Sub OnLoad(e As EventArgs)
        
    MyBase.OnLoad(e)
        
    Application.AddMessageFilter(MouseMoveFilter)
        
    AddHandler MouseMoveFilter.MouseMoving, AddressOf MouseMoving
        MouseMoveFilter.Tmr.Interval = 1000
        MouseMoveFilter.Tmr.Start()
      
    End Sub
      
    Sub MouseMoving(Moving As Boolean, Ctrl As String)
        
    Me.Text = If(Moving, "Moving over " + Ctrl, "Not Moving")
      
    End Sub
      
    Class FilterMouseMove
        
    Implements IMessageFilter
        
    Friend Event MouseMoving(Moving As Boolean, ControlName As String)
        
    Friend WithEvents Tmr As New Timer
        
    Public Function PreFilterMessage(ByRef m As MessageAs Boolean _
                                         
    Implements IMessageFilter.PreFilterMessage
          
    If m.Msg = 512 Then
            
    RaiseEvent MouseMoving(TrueControl.FromHandle(m.HWnd).Name)
            Tmr.Start()
          
    End If
          
    Return False
        
    End Function
        
    Private Sub Tmr_Tick(sender As Object, e As EventArgsHandles Tmr.Tick
          Tmr.Stop()
          
    RaiseEvent MouseMoving(False"")
        
    End Sub
      
    End Class
    End Class


    Add some controls and move the cursor over the controls.


    • Edited by JohnWein Thursday, February 2, 2012 8:46 AM
    • Marked as answer by bbby A programmer Thursday, February 2, 2012 9:11 AM
    Thursday, February 2, 2012 8:16 AM
  • Thanks for your replies guys.

     

    Hi JohnWein,

     

    That code works for me. But, I don't understand what that code does. Can you elaborate that code since I am weak in event handlers.

     

    Thanks,

    Bobby


    Ayyagari
    Thursday, February 2, 2012 9:11 AM
  • Thanks for your replies guys.

     

    Hi JohnWein,

     

    That code works for me. But, I don't understand what that code does. Can you elaborate that code since I am weak in event handlers.

     

    Thanks,

    Bobby


    Ayyagari

    I  use the MouseMoving event to communicate from the child class FilterMouseMove to its parent class Form1.  This is a preferred OOP pattern for communications from chidren to parents.  The filter is the important part of the code for your question.  The rest of the code is for demo purposes.
    • Edited by JohnWein Thursday, February 2, 2012 4:48 PM
    Thursday, February 2, 2012 4:47 PM