Getting mouse position in c#

How do I get the mouse position? I want it in term of screen position.

I start my program I want to set to the current mouse position.

Location.X = ??
Location.Y = ??

Edit: This must happen before the form is created.

You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."

System.Windows.Forms.Cursor.Position Cursor Position relative to a control. var relativePoint = myControl.PointToClient(Cursor.Position); Global hooks are not supported in the .NET Framework. See Reference. If you want to handle global mouse click events have a look at this article. Processing Global Mouse and Keyboard Hooks in C#

If you don't want to reference Forms you can use interop to get the cursor position:

using System.Runtime.InteropServices;
using System.Windows; // Or use whatever point class you like for the implicit cast operator

/// <summary>
/// Struct representing a point.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
    public int X;
    public int Y;

    public static implicit operator Point(POINT point)
    {
        return new Point(point.X, point.Y);
    }
}

/// <summary>
/// Retrieves the cursor's position, in screen coordinates.
/// </summary>
/// <see>See MSDN documentation for further information.</see>
[DllImport("user32.dll")]
public static extern bool GetCursorPos(out POINT lpPoint);

public static Point GetCursorPosition()
{
    POINT lpPoint;
    GetCursorPos(out lpPoint);
    // NOTE: If you need error handling
    // bool success = GetCursorPos(out lpPoint);
    // if (!success)

    return lpPoint;
}

I'm trying to get the mouse click coordinates on a picture box and putting the information in a textbox. I'm able to get the mouse click coordinates in the form but I'm unable to get the mouse coordinates in the picturebox (nothing happens when I click in picturebox).

Cursor.Position will get the current screen poisition of the mouse (if you are in a Control, the MousePosition property will also get the same value).

To set the mouse position, you will have to use Cursor.Position and give it a new Point:

Cursor.Position = new Point(x, y);

You can do this in your Main method before creating your form.

In my window, I've an Image component. I'd like to get mouse position when mouse moves over the Image component and save it into my model. The code behind would have been : void Foo_MouseMove(objet sender, MouseEventArgs e) { model.x = e.getPosition(this.imageBox).X; model.y = e.getPosition(this.imageBox).Y; }

To answer your specific example:

// your example
Location.X = Cursor.Position.X;
Location.Y = Cursor.Position.Y;

// sample code
Console.WriteLine("x: " + Cursor.Position.X + " y: " + Cursor.Position.Y);

Don't forget to add using System.Windows.Forms;, and adding the reference to it (right click on references > add reference > .NET tab > Systems.Windows.Forms > ok)

Get cursor position with respect to the control - C# Hot Network Questions How should I deal with a player who wants to circumvent combat when the rest of the party wants to fight?

System.Windows.Forms.Control.MousePosition

Gets the position of the mouse cursor in screen coordinates. "The Position property is identical to the Control.MousePosition property."

Cursor.Position will get the current screen poisition of the mouse (if you are in a Control, the MousePosition property will also get the same value). To set the mouse position, you will have to use Cursor.Position and give it a new Point: Cursor.Position = new Point(x, y); You can do this in your Main method before creating your form.

Cursor.Position will get the current screen poisition of the mouse (if you are in a Control, the MousePosition property will also get the same value). To set the mouse position, you will have to use Cursor.Position and give it a new Point: Cursor.Position = new Point(x, y); You can do this in your Main method before creating your form.

The position of the mouse pointer is calculated relative to the specified element with the upper-left corner of element being the point of origin, 0,0. During drag-and-drop operations, the position of the mouse cannot be reliably determined through GetPosition .

Generally, it is more convenient to handle mouse events for an application on UIElement and ContentElement, rather than using the Mouse events. For details, see Input Overview . The static members of the Mouse class delegate to the primary MouseDevice of the calling thread's input manager.

Comments
  • Cursor.Position displays my tooltip way offscreen :(-
  • @Thomas Eyde: I'm guessing, but could that be because the mouse position is in screen coordinates and your tooltip position is relative to its parent window? You might need to use PointToClient.
  • Yes, that's what I had to to.
  • How to refer POINT type?
  • Add reference to System.Drawing
  • awesome solution. But you dont need to declare struct POINT. Just use Win32Interop.Structs namespace.
  • @ManpreetSinghDhillon Is Win32Interop.Structs available in .Net Core? If yes, under which nuget package / system reference?
  • @ManpreetSinghDhillon Using your own struct allows you to implicit cast it to whatever Point you use in your code, it's a bit smoother. If Win32Interop.Structs is enough for you then go sure go ahead and use it instead!
  • No need to be rude. This is an alternative to the primary answer. I just prefer this one because the other 'Cursor.Position' sounds like a text type cursor IMHO and 'MousePosition' is more obvious.
  • @Jan Dvorak sure, and yes I thought it maybe helpful. I would have said something like this "Please could you include a little further information so I can see how this may differ from the answers given previously?"
  • @JanDvorak If you think one-liners don't help (btw, they do) then its not dependent on if the question is 1 day old or 3 yr old. +1 for alternative approach.