5

Is there a way to manipulate the mouse position without using System.Windows.Forms.Cursor? Something like interop maybe?

Reason for this is that we are using a specialized .NET subset which can't include System.Windows.Forms.

svick
  • 236,525
  • 50
  • 385
  • 514
Amir Abiri
  • 8,847
  • 11
  • 41
  • 57

2 Answers2

7

oops my bad, read question too fast, heres the correct PInvoke call

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

Source: http://www.pinvoke.net/default.aspx/user32.setcursorpos

Jf Beaulac
  • 5,206
  • 1
  • 25
  • 46
  • I'm looking to set the position, so I need http://www.pinvoke.net/default.aspx/user32.setcursorpos – Amir Abiri Jun 19 '12 at 00:25
  • Actually what I really wanted is http://www.pinvoke.net/default.aspx/user32.ClipCursor, I just didn't know something like that would exist! Life is good. – Amir Abiri Jun 19 '12 at 01:06
-3
private void MoveCursor()
{
   // Set the Current cursor, move the cursor's Position,
   // and set its clipping rectangle to the form. 

   this.Cursor = new Cursor(Cursor.Current.Handle);
   Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
   Cursor.Clip = new Rectangle(this.Location, this.Size);
}
  • The following code example creates a cursor from the Current cursor's Handle, changes its position and clipping rectangle. The result is the cursor will move up and to the left 50 pixels from where it is when the code is executed. Additionally, the cursor's clipping rectangle is changed to the bounds of the form (by default it is the user's whole screen). This example requires a Form and a Button to call this code when it is clicked. – CARL ERN VINELES BANUA Jun 19 '12 at 00:34
  • 3
    The question states that the System.Windows.Forms.Cursor cannot be used. – Rohan West Jun 19 '12 at 00:48