144

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.

asked Aug 22, 2009 at 18:34
1
  • See newer answer further down which applies to today's version of .NET Framework. Commented Dec 11, 2021 at 18:41

10 Answers 10

215

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

answered Aug 22, 2009 at 18:41
Sign up to request clarification or add additional context in comments.

3 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.
104

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;
}
answered Apr 7, 2011 at 7:36

6 Comments

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!
|
26

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.

ljs
37.9k36 gold badges110 silver badges124 bronze badges
answered Aug 22, 2009 at 18:47

Comments

20

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)

answered Oct 15, 2012 at 19:55

Comments

15
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."

answered Mar 5, 2013 at 13:19

3 Comments

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.
10

To get the position look at the OnMouseMove event. The MouseEventArgs will give you the x an y positions...

protected override void OnMouseMove(MouseEventArgs mouseEv) 

To set the mouse position use the Cursor.Position property.

http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position.aspx

answered Aug 22, 2009 at 18:42

Comments

7

If you need to get current position in form's area (got experimentally), try the following:

Console.WriteLine(
 "Current mouse position in form's area is " + 
 (Control.MousePosition.X - this.Location.X - 8).ToString() +
 "x" + 
 (Control.MousePosition.Y - this.Location.Y - 30).ToString()
);

Although, 8 and 30 integers were found while experimenting. It'd be awesome if someone could explain why exactly these numbers worked.


Also, there's another variant (considering code is in a form's code-behind):

Point cp = PointToClient(Cursor.Position); // Get cursor's position according to form's area
Console.WriteLine("Cursor position: X = " + cp.X + ", Y = " + cp.Y);
answered Dec 17, 2018 at 6:26

Comments

3

Initialize the current cursor. Use it to get the position of X and Y

this.Cursor = new Cursor(Cursor.Current.Handle);
int posX = Cursor.Position.X;
int posY = Cursor.Position.Y;
answered Jul 14, 2016 at 13:21

Comments

3
 internal static class CursorPosition {
 [StructLayout(LayoutKind.Sequential)]
 public struct PointInter {
 public int X;
 public int Y;
 public static explicit operator Point(PointInter point) => new Point(point.X, point.Y); 
 }
 [DllImport("user32.dll")]
 public static extern bool GetCursorPos(out PointInter lpPoint);
 // For your convenience
 public static Point GetCursorPosition() {
 PointInter lpPoint;
 GetCursorPos(out lpPoint);
 return (Point) lpPoint;
 }

}

answered Nov 24, 2017 at 21:42

Comments

1

This answer was not eligible at the time the question was asked, but today you can use the static method Mouse.GetPosition(IInputElement) from the namespace System.Windows.Input in the PresentationCore assembly. This is valid from .NET Framework 3.0 and forward. Find more information on https://learn.microsoft.com/en-us/dotnet/api/system.windows.input.mouse.getposition?view=windowsdesktop-6.0#System_Windows_Input_Mouse_GetPosition_System_Windows_IInputElement_

Example:

// displayArea is a StackPanel and txtBoxMousePosition is
// a TextBox used to display the position of the mouse pointer.
Point position = Mouse.GetPosition(displayArea);
txtBoxMousePosition.Text = "X: " + position.X +
 "\n" +
 "Y: " + position.Y;
answered Dec 11, 2021 at 18:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.