winform form drag

  Because the built-in interface of winform is too ugly, sometimes I want to remove the title bar, write my own, and make my own UI, but I find that I can't drag it. Or, sometimes we want the entire form to be dragged with the mouse. Move, come, come, look below

#region allows the window to be dragged at will 
[DllImport( " user32.dll " )] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); Form1_MouseDown(this, e); } private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, 0x0112, 0xF012, 0); } #endregion

  It's easy to just respond to the entire MouseDown event on the form that needs to be dragged, but again, it only takes effect on the top-level controls, which is understandable. I originally thought that only the bottom-level Form needs to be added to the event. Just fine. The result does not take effect, and all events are passed to the top-level control   . So you need to handle the event of the dragged control.

. Still relatively simple.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325948378&siteId=291194637