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.
-
See newer answer further down which applies to today's version of .NET Framework.Peter Centellini– Peter Centellini2021年12月11日 18:41:03 +00:00Commented Dec 11, 2021 at 18:41
10 Answers 10
You should use System.Windows.Forms.Cursor.Position: "A Point that represents the cursor's position in screen coordinates."
3 Comments
PointToClient.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;
}
6 Comments
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.
Comments
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)
Comments
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."
3 Comments
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
Comments
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);
Comments
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;
Comments
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;
}
}
Comments
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;