CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2003
    Posts
    133

    MouseMove on every control

    I've got the code for an idle timeout on my program working, but I need to know if there's a way to make it so when the mouse moves regardless of which control the cursor is over, an event fires calling my function.

    Right now as a test I just have
    Code:
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    But I would like to avoid doing that event on every control (Form_MouseMove, Button_MouseMove, etc...), and try and consolidate it to one line, I'm assuming that's possible I just can't figure it out.

  2. #2
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MouseMove on every control

    Two ways which are common are intercepting mouse messages, or using a timer to check the position every so often. Since your are looking for an idle condition, you are likely using a timer already, set to one minute, five minutes, or whatever. That being the case, you could simply check the mouse position, and compare it to the position at the time of the last check. You can use GetCursorPos, and store the results in a Static variable. Then the next timer event will compare the last position to the current one, and if changed, we know the user is active.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  3. #3
    Join Date
    Oct 2003
    Posts
    133

    Re: MouseMove on every control

    Quote Originally Posted by WizBang
    Two ways which are common are intercepting mouse messages, or using a timer to check the position every so often. Since your are looking for an idle condition, you are likely using a timer already, set to one minute, five minutes, or whatever. That being the case, you could simply check the mouse position, and compare it to the position at the time of the last check. You can use GetCursorPos, and store the results in a Static variable. Then the next timer event will compare the last position to the current one, and if changed, we know the user is active.
    The problem is that the timer is there to count to 30 minutes since the last time the mouse was moved. Once it fires it then dumps people back to the login. That works fine providing that the mouse is moved over the form, but if they are in a control I seem to get hosed b/c it's not detecting that move.

  4. #4
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MouseMove on every control

    Quote Originally Posted by High_D
    The problem is that the timer is there to count to 30 minutes since the last time the mouse was moved. Once it fires it then dumps people back to the login. That works fine providing that the mouse is moved over the form, but if they are in a control I seem to get hosed b/c it's not detecting that move.
    That won't happen if you use GetCursorPos. It will return the coords of the cursor for the entire screen.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

  5. #5
    Join Date
    Mar 2005
    Posts
    17

    Re: MouseMove on every control

    Try this code:

    Code:
    Private Declare Function GetCursorPos Lib "user32" Alias "GetCursorPos" (lpPoint As POINTAPI) As Long
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    Const MaxIdleTime = 1800    'IdleTime before the event (in secs)
    Dim IdleTime as Long, LastKnownPoint as POINTAPI
    Dim FunctionRunning as Boolean
    
    Private Sub Form_Load()
        LastKnownPoint.X = 0
        LastKnownPoint.Y = 0
        Timer1.Interval = 1000
        FunctionRunning = False
    End Sub
    
    Private Sub tmrCheckIdle_Timer()
        Dim LatestPoint as POINTAPI
        GetCursorPos LatestPoint
        If LatestPoint = LastKnownPoint Then
            If FunctionRunning = True Then Exit Sub
            IdleTime = IdleTime + 1
            If IdleTime >= MaxIdleTime Then
                FunctionRunning = True
                Call TheFunction    'Change this to the sub you want calling when you idle for 30 mins.
            End If
        Else
            LastKnownPoint = LatestPoint
            IdleTime = 0
            FunctionRunning = False
        Endif
    End Sub
    This code will reset the idle time (in seconds) to 0 every time you move the mouse.
    When the idle time reaches the MaxIdleTime value, it calls 'TheFunction'. You need to change this to whatever the name of the procedure is that you want to be called.
    FunctionRunning is false as default, and becomes true when the MaxIdleTime is exceeded. When the mouse is moved again, FunctionRunning becomes false again to notify your procedure to stop what it's doing. During the time that FunctionRunning is true, the IdleTime does not increase and is not reset to 0. After the mouse is moved again, the last known point is updated, the idle time reset and FunctionRunning becomes false, allowing the idle time to increase.

    Hope it's useful,

    Burningmace

  6. #6
    Join Date
    Dec 2001
    Posts
    6,332

    Re: MouseMove on every control

    Burningmace: That's the basic idea, though it's not necessary to check every second. If it were my program, I'd have the timer interval set to 1 minute (60000). Of course, this means changing the MaxIdleTime to 30, but it looks like the rest will work just as it is.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

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

Featured