943,163 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 2041
  • VB.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 22nd, 2010
0

Tracking mouse movement in VB

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kristensen292 is offline Offline
10 posts
since Jan 2010
Jan 22nd, 2010
0
Re: Tracking mouse movement in VB
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kristensen292 is offline Offline
10 posts
since Jan 2010
Jan 22nd, 2010
0
Re: Tracking mouse movement in VB
you can track the mouse by using this method
=======================================
VB.NET Syntax (Toggle Plain Text)
  1. Public Function GetMousePos() As Point
  2. Return Cursor.Position
  3. End Function
=======================================
this will return the position of the mouse on the screen.
Last edited by adatapost; Jan 23rd, 2010 at 6:58 am. Reason: Added [code] tags. Encase your code in: [code] and [/code] tags.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BoemaN is offline Offline
8 posts
since Jan 2010
Jan 23rd, 2010
0
Re: Tracking mouse movement in VB
Thanks, but i'll also need it to make a pop-up when tho mouse has moved an inch (ex)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kristensen292 is offline Offline
10 posts
since Jan 2010
Jan 23rd, 2010
0
Re: Tracking mouse movement in VB
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.

VB.NET Syntax (Toggle Plain Text)
  1. Private Shared _savedMousePosition As Point
  2.  
  3. Public Shared Sub Main()
  4. 'We save the current position when the app starts
  5. 'so the popup will not popup directly
  6. _savedMousePosition = Cursor.Position
  7. Call New Thread(AddressOf MouseCheck).Start()
  8. Process.GetCurrentProcess.WaitForExit()
  9. End Sub
  10.  
  11. Private Shared Sub MouseCheck()
  12. 'We are doing a loop here to check if the mouse has moved a certain amount of pixels
  13. 'if so then show the popup and reset the saved position.
  14. While True
  15. 'First check if the current mouse position has a distance of 20pixels from the saved position
  16. 'we use the > operator because the distance will almost never be exactly 20Pixels (i mean 20.000000)
  17. If DistanceBetween(_savedMousePosition, Cursor.Position) > 20 Then
  18. MsgBox("Mouse moved 20 pixels!")
  19.  
  20. 'here we reset the saved position to the current mouseposition and
  21. 'start all over again...
  22. _savedMousePosition = Cursor.Position
  23. End If
  24. 'we dont need a lot of checks this will only couse a high CPU usage
  25. 'so lets check the position 60 times a second
  26. Thread.Sleep(1000 / 60)
  27. End While
  28. End Sub
  29.  
  30. Private Shared Function DistanceBetween(ByVal Point1 As Point, ByVal Point2 As Point) As Single
  31. Return Math.Sqrt((Math.Abs(Point2.X - Point1.X) ^ 2) + (Math.Abs(Point2.Y - Point1.Y) ^ 2))
  32. End Function
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BoemaN is offline Offline
8 posts
since Jan 2010
Jan 23rd, 2010
0
Re: Tracking mouse movement in VB
do i just need to paste that code in the Form1 string?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kristensen292 is offline Offline
10 posts
since Jan 2010
Jan 23rd, 2010
0
Re: Tracking mouse movement in VB
you can copy the MouseCheck and distance method into the class of your form and the content of the Main method to Form1_Load
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BoemaN is offline Offline
8 posts
since Jan 2010
Jan 23rd, 2010
0
Re: Tracking mouse movement in VB
okay, thank you very much (:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kristensen292 is offline Offline
10 posts
since Jan 2010
Jan 23rd, 2010
0
Re: Tracking mouse movement in VB
it brings an error up om this line
Thread.Sleep(1000 / 60)
what cna i do there ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kristensen292 is offline Offline
10 posts
since Jan 2010
Jan 23rd, 2010
0
Re: Tracking mouse movement in VB
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:
VB.NET Syntax (Toggle Plain Text)
  1. Public Class Form1
  2. Private Shared _savedMousePosition As Point
  3.  
  4. Private Shared Sub MouseCheck()
  5.  
  6. 'We are doing a loop here to check if the mouse has moved a certain amount of pixels
  7.  
  8. 'if so then show the popup and reset the saved position.
  9.  
  10. While True
  11.  
  12. 'First check if the current mouse position has a distance of 20pixels from the saved position
  13.  
  14. 'we use the > operator because the distance will almost never be exactly 20Pixels (i mean 20.000000)
  15.  
  16. If DistanceBetween(_savedMousePosition, Cursor.Position) > 20 Then
  17.  
  18. MsgBox("Mouse moved 20 pixels!")
  19.  
  20.  
  21.  
  22. 'here we reset the saved position to the current mouseposition and
  23.  
  24. 'start all over again...
  25.  
  26. _savedMousePosition = Cursor.Position
  27.  
  28. End If
  29.  
  30. 'we dont need a lot of checks this will only couse a high CPU usage
  31.  
  32. 'so lets check the position 60 times a second
  33.  
  34. Thread.Sleep(1000 / 60)
  35.  
  36. End While
  37.  
  38. End Sub
  39.  
  40.  
  41.  
  42. Private Shared Function DistanceBetween(ByVal Point1 As Point, ByVal Point2 As Point) As Single
  43.  
  44. Return Math.Sqrt((Math.Abs(Point2.X - Point1.X) ^ 2) + (Math.Abs(Point2.Y - Point1.Y) ^ 2))
  45.  
  46. End Function
  47.  
  48. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  49.  
  50. Public Shared Sub Main()
  51.  
  52. 'We save the current position when the app starts
  53.  
  54. 'so the popup will not popup directly
  55.  
  56. _savedMousePosition = Cursor.Position
  57.  
  58. Call New Thread(AddressOf MouseCheck).Start()
  59.  
  60. Process.GetCurrentProcess.WaitForExit()
  61.  
  62.  
  63. End Sub
  64. End Class
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Kristensen292 is offline Offline
10 posts
since Jan 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: Mouse pop-up when moved
Next Thread in VB.NET Forum Timeline: How to update Table 1 out of 3 tables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC