How to get current mouse coordination on the screen?
I know only Mouse.GetPosition()
which get mousePosition of element, but I want to get the coordination without using element.

- 2,286
- 1
- 17
- 19

- 6,323
- 14
- 40
- 53
-
Mouse coordinates relative to what? Screen coordinates, relative to a Window? – Fredrik Hedblad Nov 19 '10 at 16:19
-
3I means mouse position on the screen. – Prince OfThief Nov 19 '10 at 18:10
-
`System.Windows.Forms.Cursor.Position` – vinsa Mar 23 '18 at 00:31
9 Answers
Or in pure WPF use PointToScreen.
Sample helper method:
// Gets the absolute mouse position, relative to screen
Point GetMousePos() => _window.PointToScreen(Mouse.GetPosition(_window));

- 3,249
- 3
- 24
- 42

- 2,286
- 1
- 17
- 19
-
"Converts a Point that represents the current coordinate system of the Visual into a Point in screen coordinates.". What does this have to do with mouse position? – Jonas Elfström Sep 17 '13 at 08:50
-
19Mouse.GetPosition returns a Point, and PointToScreen converts the point to the screen coordinate. – erikH Sep 19 '13 at 11:06
-
-
To follow up on Rachel's answer.
Here's two ways in which you can get Mouse Screen Coordinates in WPF.
1.Using Windows Forms. Add a reference to System.Windows.Forms
public static Point GetMousePositionWindowsForms()
{
var point = Control.MousePosition;
return new Point(point.X, point.Y);
}
2.Using Win32
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);
[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
public Int32 X;
public Int32 Y;
};
public static Point GetMousePosition()
{
var w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
return new Point(w32Mouse.X, w32Mouse.Y);
}

- 3,249
- 3
- 24
- 42

- 83,499
- 23
- 264
- 266
-
3While correct, if you take the raw X and Y coordinates and set the location of a window using them, it will not work properly if your DPI settings are anything but 100% (96dpi). The last answer to the question has it correct! – user195275 Aug 21 '17 at 21:15
-
Your comment is completely true and this should not be marked as the correct answer of this question! – Arash Sep 29 '17 at 21:10
-
Also this is a Windows Forms answer not a WPF answer. This is not the answer to the questions. – Mike May 03 '23 at 14:12
Do you want coordinates relative to the screen or the application?
If it's within the application just use:
Mouse.GetPosition(Application.Current.MainWindow);
If not, I believe you can add a reference to System.Windows.Forms
and use:
System.Windows.Forms.Control.MousePosition;
-
2
-
`error CS0234: The type or namespace name 'Control' does not exist in the namespace 'System.Windows.Forms' (are you missing an assembly reference?)` – Jonathan Tuzman Jul 21 '20 at 14:02
If you try a lot of these answers out on different resolutions, computers with multiple monitors, etc. you may find that they don't work reliably. This is because you need to use a transform to get the mouse position relative to the current screen, not the entire viewing area which consists of all your monitors. Something like this...(where "this" is a WPF window).
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var mouse = transform.Transform(GetMousePosition());
public System.Windows.Point GetMousePosition()
{
var point = Forms.Control.MousePosition;
return new Point(point.X, point.Y);
}

- 3,249
- 3
- 24
- 42

- 12,264
- 17
- 113
- 208
-
1Great thanks! I was having issues with High Resolution screens and the transformation seems to be working wonderfully. – mdiehl13 Aug 28 '14 at 00:36
-
-
1@mdiehl13 Clearly, a lot of people test just the base case. +1 for testing around different resolutions and such. :) – Alexandru Aug 28 '14 at 13:42
This works without having to use forms or import any DLLs:
using System.Windows;
using System.Windows.Input;
/// <summary>
/// Gets the current mouse position on screen
/// </summary>
private Point GetMousePosition()
{
// Position of the mouse relative to the window
var position = Mouse.GetPosition(Window);
// Add the window position
return new Point(position.X + Window.Left, position.Y + Window.Top);
}

- 2,518
- 13
- 37
- 49

- 114
- 1
- 3
You may use combination of TimerDispatcher (WPF Timer analog) and Windows "Hooks" to catch cursor position from operational system.
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT pPoint);
Point is a light struct
. It contains only X, Y fields.
public MainWindow()
{
InitializeComponent();
DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += new EventHandler(timer_tick);
dt.Interval = new TimeSpan(0,0,0,0, 50);
dt.Start();
}
private void timer_tick(object sender, EventArgs e)
{
POINT pnt;
GetCursorPos(out pnt);
current_x_box.Text = (pnt.X).ToString();
current_y_box.Text = (pnt.Y).ToString();
}
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
This solution is also resolving the problem with too often or too infrequent parameter reading so you can adjust it by yourself. But remember about WPF method overload with one arg which is representing ticks
not milliseconds
.
TimeSpan(50); //ticks

- 372
- 4
- 14
If you're looking for a 1 liner, this does well.
new Point(Mouse.GetPosition(mWindow).X + mWindow.Left, Mouse.GetPosition(mWindow).Y + mWindow.Top)
The + mWindow.Left
and + mWindow.Top
makes sure the position is in the right place even when the user drags the window around.

- 853
- 2
- 14
- 38

- 31
- 2
Mouse.GetPosition(mWindow)
gives you the mouse position relative to the parameter of your choice.
mWindow.PointToScreen()
convert the position to a point relative to the screen.
So mWindow.PointToScreen(Mouse.GetPosition(mWindow))
gives you the mouse position relative to the screen, assuming that mWindow
is a window(actually, any class derived from System.Windows.Media.Visual
will have this function), if you are using this inside a WPF window class, this
should work.

- 11
- 2
I wanna use this code
Point PointA;
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e) {
PointA = e.MouseDevice.GetPosition(sender as UIElement);
}
private void Button_Click(object sender, RoutedEventArgs e) {
// use PointA Here
}

- 1,711
- 1
- 13
- 16