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>
This code contains the following key elements:
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!