Hello
i have a problem, i need the code for tracking the mouse movement in VB 2008.
my program needs to make a pop-up window when the mouse have moven an inch ( ex.)

some code suggestions is needed, thank you

- Martin

Recommended Answers

All 12 Replies

Hello
i have a problem, i need the code for tracking the mouse movement in VB 2008.
my program needs to make a pop-up window when the mouse have moven an inch ( ex.)

some code suggestions is needed, thank you

- Martin

And it needs to go out side of the form

you can track the mouse by using this method
=======================================

Public Function GetMousePos() As Point
        Return Cursor.Position
End Function

=======================================
this will return the position of the mouse on the screen.

Thanks, but i'll also need it to make a pop-up when tho mouse has moved an inch (ex)

if you want to do that, You need to make some kind of a loop
you can create loops in different ways but i like to create a thread which contains a loop.

Private Shared _savedMousePosition As Point

    Public Shared Sub Main()
        'We save the current position when the app starts
        'so the popup will not popup directly
        _savedMousePosition = Cursor.Position
        Call New Thread(AddressOf MouseCheck).Start()
        Process.GetCurrentProcess.WaitForExit()
    End Sub

    Private Shared Sub MouseCheck()
        'We are doing a loop here to check if the mouse has moved a certain amount of pixels
        'if so then show the popup and reset the saved position.
        While True
            'First check if the current mouse position has a distance of 20pixels from the saved position
            'we use the > operator because the distance will almost never be exactly 20Pixels (i mean 20.000000)
            If DistanceBetween(_savedMousePosition, Cursor.Position) > 20 Then
                MsgBox("Mouse moved 20 pixels!")

                'here we reset the saved position to the current mouseposition and
                'start all over again...
                _savedMousePosition = Cursor.Position
            End If
            'we dont need a lot of checks this will only couse a high CPU usage
            'so lets check the position 60 times a second
            Thread.Sleep(1000 / 60)
        End While
    End Sub

    Private Shared Function DistanceBetween(ByVal Point1 As Point, ByVal Point2 As Point) As Single
        Return Math.Sqrt((Math.Abs(Point2.X - Point1.X) ^ 2) + (Math.Abs(Point2.Y - Point1.Y) ^ 2))
    End Function

do i just need to paste that code in the Form1 string?

you can copy the MouseCheck and distance method into the class of your form and the content of the Main method to Form1_Load

okay, thank you very much (:

it brings an error up om this line
Thread.Sleep(1000 / 60)
what cna i do there ?

the problems i get:
Thread.Sleep(1000 / 60)
- name "Thread" is not declared

Public Shared Sub Main()
-satatement cannot appear within a method body, end og method assumed

Call New Thread(AddressOf MouseCheck).Start()
- type "thread" is not defined

Here is my code:

Public Class Form1
    Private Shared _savedMousePosition As Point

    Private Shared Sub MouseCheck()

        'We are doing a loop here to check if the mouse has moved a certain amount of pixels

        'if so then show the popup and reset the saved position.

        While True

            'First check if the current mouse position has a distance of 20pixels from the saved position

            'we use the > operator because the distance will almost never be exactly 20Pixels (i mean 20.000000)

            If DistanceBetween(_savedMousePosition, Cursor.Position) > 20 Then

                MsgBox("Mouse moved 20 pixels!")



                'here we reset the saved position to the current mouseposition and

                'start all over again...

                _savedMousePosition = Cursor.Position

            End If

            'we dont need a lot of checks this will only couse a high CPU usage

            'so lets check the position 60 times a second

            Thread.Sleep(1000 / 60)

        End While

    End Sub



    Private Shared Function DistanceBetween(ByVal Point1 As Point, ByVal Point2 As Point) As Single

        Return Math.Sqrt((Math.Abs(Point2.X - Point1.X) ^ 2) + (Math.Abs(Point2.Y - Point1.Y) ^ 2))

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Public Shared Sub Main()

        'We save the current position when the app starts

        'so the popup will not popup directly

        _savedMousePosition = Cursor.Position

        Call New Thread(AddressOf MouseCheck).Start()

        Process.GetCurrentProcess.WaitForExit()


    End Sub
End Class

Import System.Threading
or change the Thread to Threading.Thread

Import System.Threading
or change the Thread to Threading.Thread

It works now, thank you again (:!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.