472,958 Members | 2,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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
4 2848
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jay | last post by:
Hi, How can I capture mouse position on Image? I found number of script capturing mouse position of the page. But I could not find anything based on image. What I want to find out is X Y...
4
by: Jonne | last post by:
Hi, I haven't found anything like this anywhere with Google, so I'm posting it here, hoping one of you people knows how to do something like this. I'm trying to get the mouse coordinates in a div,...
2
by: Robin Senior | last post by:
Hi, I'm trying to drag and drop onto a Panel on my form. The panel is inside a groupBox, which of course is inside my form. When dropping the item onto my Panel, I want it to appear at that...
0
by: Henry C. Wu | last post by:
Hi, I have a form that has a video "inserted" at the form's Handle. Like so: '//Create Capture Window capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100) '// Retrieves driver info lwndC =...
2
by: John | last post by:
Can Someone Please tell me why my script works in IE, but not in any other browser>???? Thanks. __________________________________________ var IE = document.all?true:false; if (!IE)...
4
by: Luongo | last post by:
Hi, I'm working on a project in which I'd like to have the user's mouse click coordinates included in a php URL which would load onClick, for example http://...imagecreate.php?x=200&y=100. I've...
4
by: atn2002 | last post by:
How can I track the mouse coordinates outside the active window? No one can tell me its not possible because Google Spreadsheets and EditGrid both do it. When you drag down to select cells these...
2
by: cefrancke | last post by:
Is there a way to capture all relevant info about the mouse, without using Mouse Up/Down etc. procedures? I'm trying to make a custom function and send it the mouse info on the click event. ...
4
by: mbatestblrock | last post by:
I hope this makes some sense. My ultimate goal here is to execute a block of code if the mouse has not moved in a minute or so within the broswer. The machine I am running this on is for internal...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.