Martin Akbaev
Martin Akbaev

Reputation: 37

How to get cursor coordinates on desktop, not at the form using C#

All that I find works only when the cursor is inside the form. How to make it possible to find out the coordinates of the cursor anywhere on the screen? It works only if cursor is inside form:

Upvotes: 1

Views: 958

Answers (3)

Martin Akbaev
Martin Akbaev

Reputation: 37

My problem was that I used a MouseMove event that doesn't work outside the form. Solved the problem using a timer.

Upvotes: 0

Victor
Victor

Reputation: 8915

To get the mouse screen coordinates:

public static Point GetMouseScreenPosition()
{
    return Control.MousePosition;
}

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.mouseposition?view=netframework-4.8

Upvotes: 1

Hogan
Hogan

Reputation: 70523

Here is how you get the bounds of all screens

// For each screen, add the screen properties to a list box.
    foreach (var screen in System.Windows.Forms.Screen.AllScreens)
    {
        listBox1.Items.Add("Device Name: " + screen.DeviceName);
        listBox1.Items.Add("Bounds: " + 
            screen.Bounds.ToString());
        listBox1.Items.Add("Type: " + 
            screen.GetType().ToString());
        listBox1.Items.Add("Working Area: " + 
            screen.WorkingArea.ToString());
        listBox1.Items.Add("Primary Screen: " + 
            screen.Primary.ToString());
    }

From the documentation https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.screen?view=netframework-4.8

as others have said to get the point in the screen you use Cursor.Position. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.cursor.position?view=netframework-4.8

Basically you want to read all the documentation on the Screen object.

Upvotes: 2

Related Questions