Home > Backend Development > C++ > How to Create a Resizable and Movable Borderless Form in Windows Forms?

How to Create a Resizable and Movable Borderless Form in Windows Forms?

Mary-Kate Olsen
Release: 2025-01-11 13:21:42
Original
635 people have browsed it

How to Create a Resizable and Movable Borderless Form in Windows Forms?

Create resizable and movable borderless forms in Windows Forms

In Windows Forms, borderless forms can be easily created by setting the "FormBorderStyle" property to "None". However, this default action disables the ability to resize the form. To overcome this limitation, more advanced methods are required.

The following code demonstrates a custom solution that simultaneously moves and resizes a borderless form:

<code class="language-csharp">public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.FormBorderStyle = FormBorderStyle.None;
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
    }

    private const int cGrip = 16;      // 调整大小控制柄大小
    private const int cCaption = 32;   // 标题栏高度

    protected override void OnPaint(PaintEventArgs e)
    {
        // 在右下角绘制调整大小控制柄
        Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
        ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);

        // 绘制模拟标题栏
        rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
        e.Graphics.FillRectangle(Brushes.DarkBlue, rc);
    }

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84) {  // 捕获WM_NCHITTEST消息
            Point pos = new Point(m.LParam.ToInt32());
            pos = this.PointToClient(pos);
            if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
            {
                m.Result = (IntPtr)17; // HTBOTTOMRIGHT
                return;
            }
        }
        base.WndProc(ref m);
    }
}</code>
Copy after login

Explanation of how the code works:

  • Custom title bar: OnPaintThe overridden method draws a blue bar at the top of the form to simulate a title bar.
  • Resize handle: The DrawSizeGrip function draws a control handle in the lower right corner of the form, allowing dragging for resizing.
  • WM_NCHITTEST event: WndProc Override method to intercept WM_NCHITTEST message to detect mouse cursor position. If the cursor is within the title bar or control handle, the appropriate hit test code is returned. The pos.Y in the code should be changed to pos.X to be logical and ensure that the resize handle area in the lower right corner is correctly detected.

The above is the detailed content of How to Create a Resizable and Movable Borderless Form in Windows Forms?. 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