How to get current mouse screen coordinates in WPF?

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

+57
wpf mouse-coordinates
Nov 19 '10 at 15:57
source share
9 answers

Trace Rachel's answer.
Here are two ways in which you can get mouse screen coordinates in WPF.

1. Using Windows Forms. Add a link to System.Windows.Forms

 public static Point GetMousePositionWindowsForms() { System.Drawing.Point 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() { Win32Point w32Mouse = new Win32Point(); GetCursorPos(ref w32Mouse); return new Point(w32Mouse.X, w32Mouse.Y); } 
+62
Nov 20 '10 at
source share

Or in pure WPF use PointToScreen .

Example helper method:

 // Gets the absolute mouse position, relative to screen Point GetMousePos(){ return _window.PointToScreen(Mouse.GetPosition(_window)) } 
+63
Apr 26 2018-12-12T00:
source share

Do you need coordinates relative to the screen or application?

If the application just uses:

 Mouse.GetPosition(Application.Current.MainWindow); 

If not, I believe that you can add a link to System.Windows.Forms and use:

 System.Windows.Forms.Control.MousePosition; 
+26
Nov 19 '10 at 16:19
source share

If you try many of these answers at different resolutions, computers with multiple monitors, etc., you may find that they do not work reliably. This is because you need to use the transform to get the mouse position relative to the current screen, and not the entire viewing area, which consists of all your monitors. Something like this ... (where "this" is the WPF window).

 var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice; var mouse = transform.Transform(GetMousePosition()); public System.Windows.Point GetMousePosition() { System.Drawing.Point point = System.Windows.Forms.Control.MousePosition; return new System.Windows.Point(point.X, point.Y); } 
+18
Nov 05 '13 at 14:03
source share

This works without using forms or importing 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); } 
+4
Jan 02 '18 at 15:58
source share

You can use a combination of TimerDispatcher (an analogue of the WPF timer) and Windows "Hooks" to catch the cursor position in the operating system.

  [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool GetCursorPos(out POINT pPoint); 

A point is an easy struct . It contains only the fields X, Y.

  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 also solves the problem of reading parameters too often or too rarely, so you can configure it yourself. But remember to overload the WPF method with a single argument representing ticks not milliseconds .

 TimeSpan(50); //ticks 
+1
Mar 04 '18 at 20:49
source share

If you are looking for 1 liner, that’s good.

 new Point(Mouse.GetPosition(mWindow).X + mWindow.Left, Mouse.GetPosition(mWindow).Y + mWindow.Top) 
+1
Oct 02 '18 at 4:52
source share

I add this for use with controls

  private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var mousepos = new List<double> { e.GetPosition(ControlName).X, e.GetPosition(ControlName).Y }; } 
0
Dec 22 '18 at 11:19
source share

Mouse.GetPosition(mWindow) gives you the position of the mouse relative to the parameter of your choice. mWindow.PointToScreen() converts the position to a point relative to the screen.

Thus, mWindow.PointToScreen(Mouse.GetPosition(mWindow)) gives you the mouse position relative to the screen, assuming mWindow is a window. If you use this inside a WPF window class, this should work

0
May 21 '19 at 8:57
source share



All Articles