Home > Backend Development > C++ > How Can I Make a Borderless Form Movable in C#?

How Can I Make a Borderless Form Movable in C#?

Linda Hamilton
Release: 2025-01-24 15:31:11
Original
675 people have browsed it

How Can I Make a Borderless Form Movable in C#?

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template