Get mouse x,y coordinates in a panel control

jake007

New member
Joined
Aug 7, 2006
Messages
2
Programming Experience
5-10
Title says it all. Anybody know how to get the mouse x,y coords. inside a panel control? (or anywhere else for that matter)

Jake
 
There is a way to get the cursor position anytime...

VB.NET:
Cursor.Position
You may need to use the PointToClient method to get it in the co-ordinates of the panel.
 
Tried that, when the cursor moves over the panel (or any control) the position the of the cursor appears unavaiable.

Jake
 
use the cursor property of the panel itself...

VB.NET:
Panel.Cursor.Position

EDIT: Incidently if you want to get the position of the cursor absolutely anywhere or anytime then plonk a timer on your form set it's interval to about 10 then put this in the tick event..

VB.NET:
Dim PNTAPI As POINTAPI
Me.GetCursorPos(PNTAPI)
Me.Label1.Text = "X:" & PNTAPI.x.ToString & " " & "Y:" & PNTAPI.y.ToString

Then this somewhere in the class....

VB.NET:
<StructLayout(LayoutKind.Sequential)> _
Private Structure POINTAPI
Public x As Int32
Public y As Int32
End Structure
Private Declare Function GetCursorPos Lib "user32.dll" (ByRef lpPoint As POINTAPI) As Int32

Dont forget to import the System.Runtime.InteropServices namespace
 
Last edited:
Thinking about it, if you are only interested about the coords while the mouse is over the panel, you could use this:

VB.NET:
        Dim pLocClient As Point = Nothing
        Dim pLocScreen As Point = Nothing

        pLocClient = Panel1.PointToClient(e.Location)
        pLocScreen = Panel1.PointToScreen(e.Location)

        Label1.Text = "Panel Coords: X: " & e.X.ToString & " Y: " & e.Y.ToString
        Label2.Text = "Client Coords: X: " & pLocClient.X.ToString & " Y: " & pLocClient.Y.ToString
        Label3.Text = "Screen Coords: X: " & pLocScreen.X.ToString & " Y: " & pLocScreen.Y.ToString

This just gets the coords for the mouse position relative to hte panel, the client and the screen :)
 
Back
Top