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

How to Resize a Borderless Windows Form?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-01-11 13:16:42205browse

How to Resize a Borderless Windows Form?

Handling resizing of borderless forms

Moving a borderless Windows Form is easy, but resizing such a form presents a unique challenge. By setting the "FormBorderStyle" property to "None", the default border disappears, making resizing impossible.

Solution:

To overcome this problem, use a custom drawn control in the lower right corner of the form, simulating a resize handle. Additionally, implement the "WndProc" method to intercept the "WM_NCHITTEST" message and determine the cursor's position relative to the form. If the cursor is inside the simulation's title bar or handle, the "m.Result" value is updated accordingly.

Here is a sample code snippet:

<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>

With these modifications, your borderless window can now be easily moved and resized.

The above is the detailed content of How to Resize a Borderless Windows Form?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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