473,474 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2878
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.