A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Get screen cursor coordinatesLearn how to retrieve the mouse cursor coordinates not just inside the form, but also when the cursor is located outside of the form, such as on the desktop or when a different application has the focus. We use the GetCursorPos() function from the Windows API. |
On Saturday, October 15th 2005 at 12:17 PM By Andrew Pociu (View Profile) ![]() ![]() ![]() ![]() (Rated 4.4 with 27 votes) |
||||||
Form-wide coordinates versus screen coordinatesUsing .NET Framework 1.1 or 2.0, retrieving the mouse coordinates is pretty easy, by making use of a couple of properties, as I proven in the tutorial named Mouse Coordinates. However, .NET Framework doesn't offer us the possibility to retrieve the mouse coordinates when the mouse leaves the form, and thus the coordinates that are relative to the screen. There are many cases in which you'll want them to retrieve the overall cursor coordinates: starting from the upper-left edge of the screen, to the most extreme point, which is the lower-right edge. There is a function in the Windows API which allows us to do just that: GetCursorPos(). Below is a screenshot of the application that I built for this tutorial and which you can download from the link at the top of the project. It's compiled using .NET 2.0, but you can compile the code with a .NET 1.1 compiler without any changes whatsoever. ![]() Using GetCursorPos() to retrieve the coordinatesTo retrieve the global mouse coordinates we need to use unmanaged code, thus add the following using statement:
GetCursorPost() is located inside user32.dll, so we need to define the attribute:
Add two labels to the form (lblCoordX and lblCoordY), where the X and Y coordinates will be displayed. Also add a timer and set its interval (the Interval property) to 10. But why do we need a timer anyway? Well, there doesn't seem to be a MouseMove event that fires when the cursor is moving, indifferent of the focused application. There is such an event in .NET, but it's only effective while the mouse hovers the form or some other control in it. So to continously monitor the cursor position we use a timer that gets the mouse cursor coordinates at a short interval (such as 10 milliseconds). After adding the timer, double click it so that we get to its single event - Tick - and use the following piece of code which calls the Windows API function and updates the two labels:
Counting traveled pixelsTrying to make this application fun, I decided to calculate the total pixels the mouse travels, more exactly to make a counter. First declare 5 more variables:
Add one more label to the Form (lblTravel), and a button (btnReset). Now inside the Tick event of the Timer, after the labels showing the X and Y coordinates are updated, use the following code:
Now to complete the application, double click the btnReset button to get to the Click event , and use the following line to reset the counter variable:
|
|||||||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||||||
|
|||||||
Current Commentshow do you check for mouse clicks?
Awesome article. I have been looking for this for a while now.
I'm in the same boat as Greg. I need to know how to get mouse clicks.
I need to get a specific cursor point on the screen when they user clicks somewhere on the screen.
Excellent! Just what I needed.
Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.
You could use the Math.Abs method instead of checking if the number is negative:
...
diffX = Math.Abs(defPnt.X - currX);
diffY = Math.Abs(defPnt.Y - currY);
// Add to the "pixels traveled" counter
...
if (diffX != defPnt.X | diffY != defPnt.Y)
is wrong, should be:
if (currX != defPnt.X | currY != defPnt.Y)
Otherwise it runs every time once the timer has started.
Could this be converted to a console app and have the output displayed to the console window?
Thanks.
Yes, since this does not retrieve the coordinates relative to a form, but relative to the entire desktop via GetCursorPos().
did you try low-level hooks?
http://www.codeproject.com/csharp/globalhook.asp
how do you have the coordinates (X, Y) with decimal places, instead of being a integer?
The coordinates are exact integers as they relate to the number of pixels on the screen. Therefore you will never have a coordinate that is 879.31 for instance, so you don't need to retrieve the decimal places. The decimal places will always be zeros.
Can I add SetcurPos() in a similar way to change cursor position with my code?
what namespaces do i need to use?
.NET C# VS 2008
Yes, you can use SetCursorPos In a very similar way. pinvoke.net has a nice example page:
http://www.pinvoke.net/default.aspx/user32/SetCursorPos.html
i need to get the cursor position depending on my C# form, not the hole screen; So i need the x and y to be 0, when i go in the left upper corner of the form, not the screen..is that posible?
I need to click button which is on another url in windows application. how can we do this ? i think by taking the co ordinates, it will be helpful ?
Please give some suggestions..
very nice job, congratulations XD
bye
can we get the mouse position without user32.dll because it does not works in patially trusted application.
timer1.Start();
to show mouse coords as it moves. Put it in Form1().
Thanks for this really helped
how do i record the mouse path and replay it?? can i store all coordinates that mouse move and replay it??
I need to get the text of icon under focus of mouse or under click in c#, what is class and method for this
I need to get the text of icon under focus of mouse or under click in c#, what is class and method for this
great code ....
very helpful ....
Thank you very much,
This is what i want, Excellent!!
Many Thank Andrew .. Works Perfect for me too..
verry good job , thx a loot :)
hi i get this one... but i want the tool tip information when any mouse event happn outside my application like click or hover events.. plz help me..
plz help me.. if my curser pointer go on any icon or any button means that name is stored in that tooltip how can i get that name to my program in c#.net ...
plz plz help me....
Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.
Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view
Related Tutorials
Related Source Code
C# Job SearchFrom the creators of Geekpedia, a revolutionary new coupon website!
BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.