Here is some simplified code to drag the WPF form around your screen. You might have seen some of this code on different posts, I just modified it to fit the needs of dragging the WPF form.
Keep in mind we need to grab the form position on the MouseLeftButtonDown, so we can keep the mouse pointer positioned in the same spot on the form as we are dragging it around the screen.
You will also need to add the following reference to get the mouse position relative to the screen: System.Windows.Forms
Properties Needed:
private bool _IsDragInProgress { get; set; }
private System.Windows.Point _FormMousePosition {get;set;}
Code:
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
this._IsDragInProgress = true;
this.CaptureMouse();
this._FormMousePosition = e.GetPosition((UIElement)this);
base.OnMouseLeftButtonDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (!this._IsDragInProgress)
return;
System.Drawing.Point screenPos = (System.Drawing.Point)System.Windows.Forms.Cursor.Position;
double top = (double)screenPos.Y - (double)this._FormMousePosition.Y;
double left = (double)screenPos.X - (double)this._FormMousePosition.X;
this.SetValue(MainWindow.TopProperty, top);
this.SetValue(MainWindow.LeftProperty, left);
base.OnMouseMove(e);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
this._IsDragInProgress = false;
this.ReleaseMouseCapture();
base.OnMouseLeftButtonUp(e);
}