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.
Get screen cursor coordinates
Learn 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)
Download this project (Visual Studio 2005)
Form-wide coordinates versus screen coordinates
Using .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 coordinates
To 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 pixels
Trying 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|
Rate this tutorial
There are no related tutorials.
Related Source Code
There is no related source code.
C# Job Search
From 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.