winforms: move a borderless form (without title bar)

234 Views Asked by At

I have a host class which inherits from Form. I use it to embed an WPF user control to use it in a winform application. This is customizable, I mean, you can choose to make form borderless or not. If borderless, you cannot move the window, so I need to implement it manually. In this case I subscribe to some event which do the trick (I have removed the not necessary things from the class to focus only on the important parts):

public class MyHostDialog : Form
{
    public MyDialogHost()
    {
        InitializeComponent();
    }

    public MyHostDialog(MyDialog myDialog) : this()
    {
        this.ElementHost.Child = new MyHostDialogView(myDialog);
        
        if (this.Borderless)
        {
            this.ElementHost.Child.MouseDown += Child_MouseDown;
            this.ElementHost.Child.MouseUp += Child_MouseUp;
            this.ElementHost.Child.MouseMove += Child_MouseMove;
        }
    }

    // BELOW CODE DOES NOT WORK, IT WOULD WORK IF Input.MouseButtonEventArgs and Input.MouseEventArgs WERE
    // System.Windows.FORMS.MouseEventArgs INSTEAD.

    private bool dragging = false;
    private System.Drawing.Point startPoint = new System.Drawing.Point(0,0);

    private void Child_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
            dragging = true;
            startPoint = new System.Drawing.Point(e.X, e.Y);
    }

    private void Child_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
            dragging = false;
    }

    private void Child_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
    {
            if (dragging)
            {
                System.Drawing.Point p= PointToScreen(e.Location);
                Location = new System.Drawing.Point(p.X - this.startPoint.X, p.Y - this.startPoint.Y);
            }
    }
}

Above code would work if instead of using Input.MouseButtonEventArgs and Input.MouseEventArgs, they were System.Windows.FORMS.MouseEventArgs. So, how to convert that code to work with MouseButtonEventArgs?

0

There are 0 best solutions below