0

I have capture mouse position from my Grid and put into TextBlock:

XAML:

<Grid MouseMove="Grid_MouseMove" SizeChanged="MainGrid_SizeChanged">    
 <TextBlock x:Name="tbMouse_X" HorizontalAlignment="Left" Margin="66,31,0,0" VerticalAlignment="Top/>
 <TextBlock x:Name="tbMouse_Y" HorizontalAlignment="Left" Margin="66,61,0,0" VerticalAlignment="Top"/>    
</Grid>

C#:

 private void Grid_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
 {
  fp_Show_Mouse_Position();
 }
 public void fp_Show_Mouse_Position()
 {
  tbMouse_X.Text = Mouse.GetPosition(this).X.ToString();
  tbMouse_Y.Text = Mouse.GetPosition(this).Y.ToString();
 }

It's working but I want to capture the position from whole my screen. How can I do that?

There are some clues about System.Windows.Forms.Cursor.Position but I want to use System.Windows.Input.Mouse.

Cœur
  • 37,241
  • 25
  • 195
  • 267
4est
  • 3,010
  • 6
  • 41
  • 63

2 Answers2

0

You could capture the mouse position across the entire screen using some P/Invoke:

public partial class MainWindow : Window
{
    private const int WH_MOUSE_LL = 14;
    private IntPtr _mouseHandle;
    private delegate IntPtr HookDelegate(int Code, IntPtr wParam, IntPtr lParam);
    private HookDelegate _mouseDelegate;

    [DllImport("User32.dll")]
    static extern IntPtr SetWindowsHookEx(int idHook, HookDelegate lpfn, IntPtr hmod, int dwThreadId);

    [DllImport("User32.dll")]
    static extern IntPtr CallNextHookEx(IntPtr hHook, int nCode, IntPtr wParam, IntPtr lParam);

    [DllImport("User32.dll")]
    static extern IntPtr UnhookWindowsHookEx(IntPtr hHook);

    [StructLayout(LayoutKind.Sequential)]
    struct POINT
    {
        public int X;
        public int Y;
    }

    [DllImport("user32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool GetCursorPos(out POINT lpPoint);

    public MainWindow()
    {
        InitializeComponent();

        _mouseDelegate = MouseHookDelegate;
        _mouseHandle = SetWindowsHookEx(WH_MOUSE_LL, _mouseDelegate, IntPtr.Zero, 0);

        Closing += (s, e) => 
        {
            if (_mouseHandle != IntPtr.Zero)
                UnhookWindowsHookEx(_mouseHandle);
        };
    }

    private IntPtr MouseHookDelegate(int code, IntPtr wParam, IntPtr lParam)
    {
        if (code < 0)
            return CallNextHookEx(_mouseHandle, code, wParam, lParam);

        POINT point;
        GetCursorPos(out point);

        tbMouse_X.Text = point.X.ToString();
        tbMouse_Y.Text = point.Y.ToString();

        return CallNextHookEx(_mouseHandle, code, wParam, lParam);
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88
  • thank you mm8, the Max Bender solution from https://stackoverflow.com/questions/4226740/how-do-i-get-the-current-mouse-screen-coordinates-in-wpf it's less complex – 4est Jul 06 '18 at 10:53
  • @4est: There is a subtle difference between that solution and mine. The other one uses a `Timer` and calls the `GetCursorPos` method every 50 ms (!) regardless of whether the mouse pointer has actually moved. The solution presented here calls `GetCursorPos` whenever the positon of the mouse pointer actually changes. You could use whichever solution you find the most appropriate. – mm8 Jul 06 '18 at 11:51
0

here is my working code based on this answer:

Also, I have added one more function: move cursor to the random position.

XAML:

<Grid>    
 <TextBlock x:Name="tbMouse_X" HorizontalAlignment="Left" Margin="66,31,0,0" VerticalAlignment="Top/>
 <TextBlock x:Name="tbMouse_Y" HorizontalAlignment="Left" Margin="66,61,0,0" VerticalAlignment="Top"/>
 <TextBlock x:Name="tbMouse_A" HorizontalAlignment="Left" Margin="206,31,0,0" VerticalAlignment="Top"/>
 <TextBlock x:Name="tbMouse_B" HorizontalAlignment="Left" Margin="206,61,0,0" VerticalAlignment="Top"/>    
</Grid>

C#:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
            dt.Tick += new EventHandler(timer_tick);
            dt.Tick += new EventHandler(move_mouse);
            dt.Interval = new TimeSpan(0, 0, 0, 0, 10000);
            dt.Start();
        }

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

        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int new_x, int new_y);

        private void timer_tick(object sender, EventArgs e)
        {
            POINT pnt;
            GetCursorPos(out pnt);
            tbMouse_X.Text = (pnt.X).ToString();
            tbMouse_Y.Text = (pnt.Y).ToString();
        }

        public void move_mouse(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int A = rnd.Next(0, 1000);
            int B = rnd.Next(0, 1000);

            SetCursorPos(A, B);

            tbMouse_A.Text = A.ToString();
            tbMouse_B.Text = B.ToString();           
        }

        public struct POINT
        {
            public int X;
            public int Y;

            public POINT(int x, int y)
            {
                this.X = x;
                this.Y = y;
            }
        }
    }
4est
  • 3,010
  • 6
  • 41
  • 63