Home > Backend Development > C++ > How to Make a Borderless WinForms Resizable?

How to Make a Borderless WinForms Resizable?

Mary-Kate Olsen
Release: 2025-01-11 13:07:44
Original
543 people have browsed it

How to Make a Borderless WinForms Resizable?

Create borderless resizable WinForms forms

Creating borderless and resizable forms is not easy in WinForms. However, with only a few modifications, it can be made borderless and resizable.

To disable the default Windows border, set the "FormBorderStyle" property to "None". But this also removes the ability to resize the form. To do this, code adjustments are required.

The following code example defines a custom handler for drawing resize handles and simulating the title bar, while overriding "WndProc" to handle the WM_NCHITTEST message:

<code class="language-c#">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

With this code you can create a borderless form that can still be resized by dragging the bottom right handle and the simulated title bar.

The above is the detailed content of How to Make a Borderless WinForms Resizable?. 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