Capture Mouse Coordinates in VS2005

R

Ryan

I have a winform containing a scrollable panel and a groupbox inside the
panel. There is a button inside the groupbox.
When that button is clicked; how do I capture and display the X and Y
coordinates in a messagebox?
 
N

Nicholas Paldino [.NET/C# MVP]

Ryan,

You can simply look at the static Position class on the Cursor class in
the System.Windows.Forms namespace. This will give you the position in
screen coordinates, which you will have to convert to your application
(which can be done on any control window by calling the PointToClient method
on the control itself).

There are some issues with this, as you are polling for the coordinate
after the mouse button is pressed, in which case, you might not get an exact
coordinate (as the mouse location can move before your code is called, and
you are polling for the coordinate).

If you need the exact coordinate, handle the MouseUp event (as that is
when the click is registered) and store the coordinates of the mouse from
the MouseEventArgs that is passed in the event handler. Then, in the Click
event, you would take those stored coordinates and work with those.
 
R

Ryan

Thanks Nicholas.
Would you, or someone else, be able provide a sample code?

Nicholas Paldino said:
Ryan,

You can simply look at the static Position class on the Cursor class in
the System.Windows.Forms namespace. This will give you the position in
screen coordinates, which you will have to convert to your application
(which can be done on any control window by calling the PointToClient method
on the control itself).

There are some issues with this, as you are polling for the coordinate
after the mouse button is pressed, in which case, you might not get an exact
coordinate (as the mouse location can move before your code is called, and
you are polling for the coordinate).

If you need the exact coordinate, handle the MouseUp event (as that is
when the click is registered) and store the coordinates of the mouse from
the MouseEventArgs that is passed in the event handler. Then, in the Click
event, you would take those stored coordinates and work with those.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ryan said:
I have a winform containing a scrollable panel and a groupbox inside the
panel. There is a button inside the groupbox.
When that button is clicked; how do I capture and display the X and Y
coordinates in a messagebox?
 
S

Stanimir Stoyanov

Hi Ryan,

What Nicholas meant is this:

MessageBox.Show(Cursor.Position.ToString());

In case you have or need to obtain the cursor position, relative to one of
your controls, you call the PointToClient method on it: e.g.

MessageBox.Show(button1.PointToClient(Cursor.Position).ToString());

Best Regards,
Stanimir Stoyanov | www.stoyanoff.info

Ryan said:
Thanks Nicholas.
Would you, or someone else, be able provide a sample code?

Nicholas Paldino said:
Ryan,

You can simply look at the static Position class on the Cursor class
in
the System.Windows.Forms namespace. This will give you the position in
screen coordinates, which you will have to convert to your application
(which can be done on any control window by calling the PointToClient
method
on the control itself).

There are some issues with this, as you are polling for the
coordinate
after the mouse button is pressed, in which case, you might not get an
exact
coordinate (as the mouse location can move before your code is called,
and
you are polling for the coordinate).

If you need the exact coordinate, handle the MouseUp event (as that
is
when the click is registered) and store the coordinates of the mouse from
the MouseEventArgs that is passed in the event handler. Then, in the
Click
event, you would take those stored coordinates and work with those.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ryan said:
I have a winform containing a scrollable panel and a groupbox inside the
panel. There is a button inside the groupbox.
When that button is clicked; how do I capture and display the X and Y
coordinates in a messagebox?
 
R

Ryan

Nicholas and Stanimir,
Thanks to both of you; it worked the way I wanted to.

Stanimir Stoyanov said:
Hi Ryan,

What Nicholas meant is this:

MessageBox.Show(Cursor.Position.ToString());

In case you have or need to obtain the cursor position, relative to one of
your controls, you call the PointToClient method on it: e.g.

MessageBox.Show(button1.PointToClient(Cursor.Position).ToString());

Best Regards,
Stanimir Stoyanov | www.stoyanoff.info

Ryan said:
Thanks Nicholas.
Would you, or someone else, be able provide a sample code?

Nicholas Paldino said:
Ryan,

You can simply look at the static Position class on the Cursor class
in
the System.Windows.Forms namespace. This will give you the position in
screen coordinates, which you will have to convert to your application
(which can be done on any control window by calling the PointToClient
method
on the control itself).

There are some issues with this, as you are polling for the
coordinate
after the mouse button is pressed, in which case, you might not get an
exact
coordinate (as the mouse location can move before your code is called,
and
you are polling for the coordinate).

If you need the exact coordinate, handle the MouseUp event (as that
is
when the click is registered) and store the coordinates of the mouse from
the MouseEventArgs that is passed in the event handler. Then, in the
Click
event, you would take those stored coordinates and work with those.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a winform containing a scrollable panel and a groupbox inside the
panel. There is a button inside the groupbox.
When that button is clicked; how do I capture and display the X and Y
coordinates in a messagebox?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top