how does one find mouse location in screen coordinate


New member
Mar 28, 2005
Programming Experience
how does one find mouse coordinates location on screen (not just in form)using VB.NET.?
Any tutorials or suggestions.
Just displaying those coordinates in text boxes is all I am trying to do.
Should i use mouse messages or get cursor positions or mousemove? I am a beginner so any tips/code samples is useful. Anyting to get me started it right direction.
Last edited:
It's really pretty simple. Just put something like this the Form's (and any controls that may be on the Form) MouseMove event:

Dim ptLocationOnForm As New System.Drawing.Point
Dim ptLOcationOnScreen As New System.Drawing.Point
'To get the mouse location relative to the Form
ptLocationOnForm = Me.PointToClient(Cursor.Position)
'To get the mouse location relative to the screen
ptLOcationOnScreen = Me.PointToScreen(Cursor.Position)
'SHow the locations
txtMouseX.Text = ptLocationOnForm.X.ToString
txtMouseY.Text = ptLocationOnForm.Y.ToString

thanks mothra for that and it does help to some degree, only what I am trying to do is be able to drag cursor all over the screen and have the form text boxes update/get the coordinates from whole desktop. ... basically like many on screen color picker programs do. From what you gave me if i used correctly is is as soon as i go outside form it is not getting/updating coordinates continously anymore. You know what I mean?
You have any other suggestions maybe?
thank you.
You'll first need to capture the mouse so your window can receive mouse messages even when the mouse is over another window. Declare the following in a module somewhere:

Public Declare Function SetCapture Lib "user32" Alias "SetCapture" (ByVal hwnd As IntPtr) As IntPtr

Then call it as follows:


After that, you just handle your mousemove event and it will contain the coordinates you need to show in your text box.

When done, you need to call the following to release the mouse (add declaration to a module somewhere):

Public Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As IntPtr

which you simply call as follows:


and that's it :)
could you you clarify this part


SetCapture(YourWindow.Handle) could you you clarify this part?
"YourWindow" refers to the other app I would be gettting mouse messages from?

Will your method recieve mouse messages even when over just the desktop? or do I have to be over another window?

Latest posts