1
votes

I have an MDI parent container (form style: none) that fits to 1280x800. Child forms fit just within, say probably 1 or 2 pixels padding between the child edge and the parent. Mean to look seamless.

I know how to make separate forms draggable (regardless of where you click and hold down on the form) but is it possible to have a child dictate to the MDI parent where to move on screen? Reason being is that there is nothing on the parent that the user could click on. We cannot add an object to the parent (like a menu) because it would conflict with the design we are going for.

Suggestions on this one? The goal would be that users could click and drag anywhere on the child and it would move the entire application.

1

1 Answers

1
votes

Try this:

public partial class Form1 : Form { 
  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();

  public Form1() {
    InitializeComponent();
    Form f = new Form();
    f.MouseDown += ChildForm_MouseDown;
    f.MdiParent = this;
    f.Show();
  }

  void ChildForm_MouseDown(object sender, MouseEventArgs e) {
    if (e.Button == MouseButtons.Left) {
      ReleaseCapture();
      SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
    }
  }
}

The MDI Parent form is listening to the child mousedown events, and when the user clicks down on the child form, it will act like the user is clicking on the title bar of the main form.