c# move window with mouse code example

Example 1: drag form in C# winform

[DllImport("user32")]
private static extern bool ReleaseCapture();

[DllImport("user32")]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wp, int lp);

protected override void OnMouseDown(MouseEventArgs e)
{
  	base.OnMouseDown(e);
	if (e.Button == MouseButtons.Left)
    {
    	ReleaseCapture();
      	SendMessage(Handle, 161, 2, 0);
    }
}

Example 2: how to move mouse in c#

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