Move borderless form using mouse
Posted on May 17, 2013 by CooLMinE 1 Comment
Forms that have their FormBorderStyle
set to None cannot be moved using the mouse by default. In order to achieve this functionally we can use the ReleaseCapture
and SendMessage
Windows functions.
Simply place the following code in your borderless form class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | const int HTCAPTION = 0x2; const int WM_NCLBUTTONDOWN = 0xA1; [DllImportAttribute("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImportAttribute("user32.dll")] public static extern bool ReleaseCapture(); private void form1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ReleaseCapture(); SendMessage(this.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } } |
Don’t forget to assign your form’s MouseDown
event to the form1_MouseDown
method !
Keep in mind that you could handle another control’s MouseDown
event (a good example would be a MenuStrip
control) instead of the form’s one, making your form moveable only when the user clicks and drags that specific control.
Amazing! You know I love your blog!!!