The accepted answer is a cool trick, but it doesn’t always work if the form is covered by a Fill-docked child, such as Panel (or derivatives), because this control will use all the majority of Windows messages.
Here is a simple approach that works in this case too: output the appropriate control (use this class instead of the standard one) for a mouse message like this:
private class MyTableLayoutPanel : Panel // or TableLayoutPanel, etc. { private Point _mouseDown; private Point _formLocation; private bool _capture; // NOTE: we cannot use the WM_NCHITTEST / HTCAPTION trick because the table is in control, not the owning form... protected override void OnMouseDown(MouseEventArgs e) { _capture = true; _mouseDown = e.Location; _formLocation = ((Form)TopLevelControl).Location; } protected override void OnMouseUp(MouseEventArgs e) { _capture = false; } protected override void OnMouseMove(MouseEventArgs e) { if (_capture) { int dx = e.Location.X - _mouseDown.X; int dy = e.Location.Y - _mouseDown.Y; Point newLocation = new Point(_formLocation.X + dx, _formLocation.Y + dy); ((Form)TopLevelControl).Location = newLocation; _formLocation = newLocation; } } }
Simon Mourier Jan 31 '14 at 17:20 2014-01-31 17:20
source share