467,891 Members | 1,848 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,891 developers. It's quick & easy.

Capture Mouse Coordinates in VS2005

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?
Jul 29 '08 #1
  • viewed: 2643
Share:
4 Replies
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]
- mv*@spam.guard.caspershouse.com

"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:74**********************************@microsof t.com...
>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?

Jul 29 '08 #2
Thanks Nicholas.
Would you, or someone else, be able provide a sample code?

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:74**********************************@microsof t.com...
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?


Jul 29 '08 #3
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.Posit ion).ToString());

Best Regards,
Stanimir Stoyanov | www.stoyanoff.info

"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:F7**********************************@microsof t.com...
Thanks Nicholas.
Would you, or someone else, be able provide a sample code?

"Nicholas Paldino [.NET/C# MVP]" wrote:
>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]
- mv*@spam.guard.caspershouse.com

"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:74**********************************@microso ft.com...
>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?


Jul 29 '08 #4
Nicholas and Stanimir,
Thanks to both of you; it worked the way I wanted to.

"Stanimir Stoyanov" wrote:
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.Posit ion).ToString());

Best Regards,
Stanimir Stoyanov | www.stoyanoff.info

"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:F7**********************************@microsof t.com...
Thanks Nicholas.
Would you, or someone else, be able provide a sample code?

"Nicholas Paldino [.NET/C# MVP]" wrote:
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]
- mv*@spam.guard.caspershouse.com

"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:74**********************************@microsof t.com...
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?

Jul 29 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Jay | last post: by
4 posts views Thread by Jonne | last post: by
2 posts views Thread by Robin Senior | last post: by
reply views Thread by Henry C. Wu | last post: by
2 posts views Thread by John | last post: by
4 posts views Thread by atn2002 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.