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);
Max Bender Mar 04 '18 at 20:49 2018-03-04 20:49
source share