1

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.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Raxdiam
  • 73
  • 2
  • 7
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Dec 13 '13 at 23:10
  • BTW, it's much more important that you're working with Windows Forms, than that you happen to be using C# as the programming language. – John Saunders Dec 13 '13 at 23:11
  • Sorry about that. I will remember this next time. – Raxdiam Dec 13 '13 at 23:13
  • Replacing *this* with Form.ActiveForm just doesn't make any sense at all. You want to use your own fields, not the form's properties. Just don't do that. – Hans Passant Dec 14 '13 at 00:19

1 Answers1

1

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();
Community
  • 1
  • 1
LarsTech
  • 80,625
  • 14
  • 153
  • 225