Create a frameless frame to move the window (C#)
Frame windows are easy to adjust the size and movement, but this is not always the best choice. Sometimes you may need a borderless design, but this will bring a problem: how can we move the window without the frame? The following is how to achieve this goal:
This method uses the Win32 API simulation belt of the title bar window. You need the following constant and functions:
In the Mousedown event processing program of the infinity window, the following code is achieved:
<code class="language-csharp">public const int WM_NCLBUTTONDOWN = 0xA1; public const int HT_CAPTION = 0x2; [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern bool ReleaseCapture();</code>
This code simulation clicks the behavior bar, allowing you to move the entire window by clicking anywhere in the window. When pressing the left mouse button, it releases the capture and sends a message to the window manager, indicating that the button on the "title bar" (HT_CAPTION) in the window is pressed. By simulating this behavior, you can move the window like a border without affecting the required borderless design.
<code class="language-csharp">private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if (e.Button == MouseButtons.Left) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0); } }</code>
The above is the detailed content of How Can I Make a Borderless Form Movable in C#?. For more information, please follow other related articles on the PHP Chinese website!