Search code examples
c#wpfkinect

Set Mouseposition in WPF


I'm going to replace my mouse by Kinect gestures but I can't find a way to set mouseposition for a WPF app.


Solution

  • You can use the Cursor.Position property found in System.Windows.Forms for this.

    As demonstrated on the MSDN documentation for Cursor.Position:

    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);
    }
    

    If you're looking to do this outside of Windows Forms, you can do a platform invoke on User32's SetCursorPos.