title: C# WPF application .NET 4.5 Set Mouse Position
body: <p>First time asking a question here, the solutions I found on here do not seem to work for some reason.  My app needs to set the mouse position when the window becomes active, I have the function set up but cannot get cursor properties to work.  I can't use Cursor.Position or anything really for some reason.  I had hoped to visit the chat rooms to find a solution but apparently I cannot speak until I have 20 reputation.</p>
  
  <p>So here I am asking how to change the cursor position with something like</p>
  
  <p>this.Cursor.SetPosition(x, y);</p>
  
  <p>Thanks for the help.</p>
  
  <p>Edit: Tried this already as a test from <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.cursor.position(v=vs.110).aspx" rel="noreferrer">here</a>:</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>but the compiler complains about Current, Position, Clip, Location, Size</p>
  
  <p>Final solution:</p>
  
  <pre><code>using System.Runtime.InteropServices;
  
  ...
  
  [DllImport("User32.dll")]
  private static extern bool SetCursorPos(int X, int Y);
  
  ...
  
  Point relativePoint = MouseCaptureButton.TransformToAncestor(this)
                             .Transform(new Point(0, 0));
  Point pt = new Point(relativePoint.X + MouseCaptureButton.ActualWidth / 2,
                       relativePoint.Y + MouseCaptureButton.ActualHeight / 2);
  Point windowCenterPoint = pt;//new Point(125, 80);
  Point centerPointRelativeToSCreen = this.PointToScreen(windowCenterPoint);
  SetCursorPos((int)centerPointRelativeToSCreen.X, (int)centerPointRelativeToSCreen.Y);
  </code></pre>