Home > Backend Development > C++ > How to Resize a Borderless WinForm?

How to Resize a Borderless WinForm?

DDD
Release: 2025-01-11 13:12:43
Original
563 people have browsed it

How to Resize a Borderless WinForm?

Creating a Resizable Borderless WinForm: The Complete Guide

When customizing the appearance of Windows Forms, you may want to remove the default borders and allow resizing. While the "FormBorderStyle" property allows the border to be hidden, it also disables resizing.

To solve this problem, consider the following custom code, which allows moving and resizing without borders:

<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

This code contains the following key elements:

  • Draw resize handles: This custom draw handle allows for resizing corner handles.
  • Draw Title Bar: Draws a simulated title bar to provide click and drag targets for moving the form.
  • Handling Windows Messages: Message handling ensures that resizing is enabled when the user clicks on the handle and the correct mouse cursor is displayed.

By implementing this code, you can now enjoy the benefits of a borderless WinForm and easily resize it.

The above is the detailed content of How to Resize a Borderless WinForm?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template