简体   繁体   中英

Drag form with control from another class

Firstly, I apologize if the title does not make much sense, as I did not know the best way to explain it.

Now to really explain it. What I have done is created a control in a Class Library project in Visual Studio 2013. This control is supposed to act as the caption bar for form that is set with the "FormBorderStyle" as "None". This imitation caption bar control is supposed to move the form, just like a normal forms' caption bar would.

I have achieved this, but only in the forms code. This is the code I use:

    private int mouseStartX, mouseStartY;
    private int formStartX, formStartY;
    private bool FormDragging = false;

    private void titleBar_MouseDown(object sender, MouseEventArgs e)
    {

        this.mouseStartX = MousePosition.X;
        this.mouseStartY = MousePosition.Y;
        this.formStartX = this.Location.X;
        this.formStartY = this.Location.Y;
        FormDragging = true;
    }

    private void titleBar_MouseMove(object sender, MouseEventArgs e)
    {
        if (FormDragging)
        {
            this.Location = new Point(
            this.formStartX + MousePosition.X - this.mouseStartX,
            this.formStartY + MousePosition.Y - this.mouseStartY
            );
        }
    }

    private void titleBar_MouseUp(object sender, MouseEventArgs e)
    {
        FormDragging = false;
    }

"this.*" is obviously referring to the form, when in the forms code. So of course, if I were to simply put this into the controls code, it'd obviously be referring to the control, and thus the control would be the one moving around on the form.

I've also created a control in the Class Library that acts as a close button. All I had to do was:

    Form.ActiveForm.Close();

Same for minimize being:

    Form.ActiveForm.WindowState = FormWindowState.Minimized;

And maximize being:

    Form.ActiveForm.WindowState = FormWindowState.Maximized;

On the controls' click events.

When I try to replace "this. " with "Form.ActiveForm. ", in the first code posted - it returns this error:

'System.Windows.Forms.Form' does not contain a definition for 'mouseStarX' and no extension method 'mousStartX' accepting a first argument of type 'System.Windows.Forms.Form' could be found (are you missing a using directive or an assembly reference?)

That's about it, I don't know how else to go about this.

There is a simple pinvoke you can use to move the form via your control.

Adapted from C# - Make a borderless form movable? , instead of using Form.ActiveForm , you would use this.FindForm() to get the parent form of the control. It's used here to pass the form's handle value:

public class MyHeader : Control {
  private const int WM_NCLBUTTONDOWN = 0xA1;
  private const int HT_CAPTION = 0x2;

  [DllImportAttribute("user32.dll")]
  private static extern int SendMessage(IntPtr hWnd, int Msg,
                                        int wParam, int lParam);
  [DllImportAttribute("user32.dll")]
  private static extern bool ReleaseCapture();

  protected override void OnMouseDown(MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
      ReleaseCapture();
      SendMessage(this.FindForm().Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
    base.OnMouseDown(e);
  }
}

For closing the form, you would use the same method:

this.FindForm().Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM