This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Obtaining the Mouse Pointer Screen Co-ordinates

Most software that uses the mouse cursor's position only needs to obtain the co-ordinates whilst the mouse pointer is within one of its windows. However, some programs require the screen co-ordinates, even if the pointer is outside of a window.


If you are developing software for Microsoft Windows, you might need to obtain the position of the mouse pointer, relative to the screen. If you want the co-ordinates of the pointer whilst it is positioned over one of your application's windows, this is quite simple to achieve. However, if your application must track the mouse pointer outside of its windows, or if your software has no window at all, the .NET framework does not provide an easy way to get the co-ordinates. Instead, you must use a Windows API function named, "GetCursorPos".

In this article we'll create a simple C# program that tracks the position of the mouse pointer. We'll use a console application to show that no Windows Forms or WPF window is required. To begin, create a new console application and ensure that the automatically generated class that contains the Main method is open.

Referencing the API Function

GetCursorPos can't be called directly from C#. We need to reference it using Platform Invocation Services (P/Invoke). In order to use P/Invoke, we'll need to use an attribute from the System.Runtime.InteropServices namespace. To simplify access to this namespace, add the following using directive to the class file:

using System.Runtime.InteropServices;

You can now add the code that defines the function. GetCursorPos returns a Boolean value that is true if the co-ordinates were correctly retrieved and false otherwise. The co-ordinates are returned as a POINT structure, using an output parameter.

Add the function definition to the class:

static extern bool GetCursorPos(out POINT lpPoint);

The POINT structure does not exist as a .NET type so we need to define it. Create a new file named "POINT.cs" and add the code for the structure:

public struct POINT
    public int X;
    public int Y;

Calling GetCursorPos

With the GetCursorPos function defined, you can call it directly from your C# code. Let's add code to the Main method and its containing class to repeatedly call the function and display the mouse pointer's screen co-ordinates.

To reduce flicker as the co-ordinates update, we'll only show the new location if it differs from the previous value. To store the old position, create two integer fields for the X and Y co-ordinates by adding the following code within the class but outside of any methods:

static int _x, _y;

We can now create a method that calls GetCursorPos and displays the co-ordinates. We need to declare a variable that will store the position before passing it to the API function. We also need to check that the function returns a true result and that the mouse pointer has moved before we update the output.

Add the following method to the class to perform the above actions:

static void ShowMousePosition()
    POINT point;
    if (GetCursorPos(out point) && point.X != _x && point.Y != _y)
        Console.WriteLine("({0},{1})", point.X, point.Y);
        _x = point.X;
        _y = point.Y;

Let's call the above method repeatedly from the Main method until a key is pressed. We can control this with a simple while loop that waits for the Console.KeyAvailable property to be set to true. We'll also hide the console's cursor until the loop ends to prevent it flashing near the displayed co-ordinates.

Add the following code to the Main method:

Console.CursorVisible = false;
while (!Console.KeyAvailable)
Console.CursorVisible = true;

Run the program and move the mouse to see the co-ordinates within the console window. Note that the text updates correctly, even if the mouse pointer is not within the console area and when other applications are activated.

21 July 2015