locked
How do I track mouse movement by virtue of its position on the SCREEN? RRS feed

  • Question

  • I tried the usual mouse events that some with VB, but that gave me the mouse's position by virtue of its position on the FORM, not the SCREEN.

    Is there any way to get the mouse's position on the screen, not form?

    Saturday, May 26, 2007 11:59 AM

Answers

  • Hello,

     

    You can add a timer with a low interval or you can use while statement.

     

    If you use the timer then the code will be:

     

    Code Snippet

     

    dim x as integer

    dim y as integer

     

    private sub timer_tick

    x= windows.forms.cursor.position.x

    y= windows.forms.cursor.position.y

      end sub

     

     

    You could attach the (x and y) to textboxes or labels.

     

    You could stop the timer when the mouse is clicked or when a key is pressed.

     

    Code Snippet

    private sub form_keydown

    select case e.keycode

    case keys.enter

    timer.enabled = false

    end select

     end sub

     

    If you have any questions reply.

    Sunday, May 27, 2007 7:18 AM
  • To get the cursor position anywhere on the screen use the Win32API function "GetCursorPos".
    Sunday, May 27, 2007 8:50 AM

All replies

  • Private Sub Form1_MouseMove(ByVal sender As Object, _
                               
    ByVal e As MouseEventArgs) _
                               
    Handles Me.MouseMove

      ' Translate the point to screen co-ords:
      Me.Text = Me.PointToScreen(e.Location).ToString
     
    ' Or, grab it from the cursor.
      Me.Text &= " | " & Cursor.Position.ToString

    End Sub
    Saturday, May 26, 2007 1:05 PM
  • Thanks, it works.

    Although, my .net 2003 (I forgot to mention that) couldnt define what e.Location was.

    It only knew e.X and e.Y.

    BTW, is there anyway to get the stats even when the mouse is outside the form?

    Sunday, May 27, 2007 5:22 AM
  • Hello,

     

    You can add a timer with a low interval or you can use while statement.

     

    If you use the timer then the code will be:

     

    Code Snippet

     

    dim x as integer

    dim y as integer

     

    private sub timer_tick

    x= windows.forms.cursor.position.x

    y= windows.forms.cursor.position.y

      end sub

     

     

    You could attach the (x and y) to textboxes or labels.

     

    You could stop the timer when the mouse is clicked or when a key is pressed.

     

    Code Snippet

    private sub form_keydown

    select case e.keycode

    case keys.enter

    timer.enabled = false

    end select

     end sub

     

    If you have any questions reply.

    Sunday, May 27, 2007 7:18 AM
  • To get the cursor position anywhere on the screen use the Win32API function "GetCursorPos".
    Sunday, May 27, 2007 8:50 AM
  • Windows doesn't send mouse messages to a form when the cursor is outside the form. This means there isn't an event built in that fires when the mouse moves outside the form. So you can poll as suggested above, or you can set up a global mouse hook:

    http://www.codeproject.com/csharp/globalhook.asp
    Sunday, May 27, 2007 11:39 AM
  •  jo0ls wrote:
    Windows doesn't send mouse messages to a form when the cursor is outside the form. This means there isn't an event built in that fires when the mouse moves outside the form. So you can poll as suggested above, or you can set up a global mouse hook:

    http://www.codeproject.com/csharp/globalhook.asp

    You missed a slight detail there.

    Monday, May 28, 2007 4:40 AM
  • Man my way is right, i always use it in my programs, and it is very useful to me.

     

    Try it!

    Monday, May 28, 2007 9:14 AM
  •  Zooz wrote:

    Man my way is right, i always use it in my programs, and it is very useful to me.

     

    Try it!

     

    You may note that I marked it as an answer.

    I didn't say thanks because I thought that would be superfluous.

    Monday, May 28, 2007 2:45 PM