0

I am working on a WPF application and want to set the cursor position to the corner of the screen.

I tried using the WinForms approach:

System.Windows.Forms.Cursor.Position = new System.Drawing.Point(0, 0);

and the user32.dll approach:

[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetCursorPos(int X, int Y);

The WinForms approach worked sometimes, but I don't understand why it worked and didn't work sometimes. I made sure the code is actually being called. The user32.dll approach didn't work for me at all.

lenny
  • 734
  • 2
  • 15
  • 43
  • ...did you ever check the return-value from `SetCursorPos`? (And you need to add `[return: MarshalAs( UnmanagedType.Bool )]`) – Dai Oct 07 '22 at 14:39
  • Did you try SetCursorPos with values higher than 0 for X and Y? Try with 100, 100. – Andy Oct 07 '22 at 15:23

1 Answers1

0

Simple solution is make your application enough big and move mouse inside of it. http://www.aspdotnet-pools.com/2017/10/move-mouse-cursor-automatically-c.html

To move mouse outside your application you need global mouse hook. Global mouse event handler