防止自定义边框调整大小控点调整大小通知太小



我目前正忙于开发一个无边框的WinForms表单,我刚刚从这里得到了一些代码。我根据自己的需要修改了它,但现在我卡住了。我将如何限制它可以调整多少大小?

目前,我可以调整窗口大小,使其只是屏幕上的一个点。如何在最小尺寸上设置"上限"?

这是用于调整器的代码片段(应按原样工作(:

//Initializes the form
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.ResizeRedraw, true);
}
private const int cGrip = 16;      // Grip size
private const int cCaption = 32;   // Caption bar height;
//Overides what happens everytime the form is drawn
protected override void OnPaint(PaintEventArgs e)
{
//Draws a border with defined width
int width = 1;
Pen drawPen = new Pen(titleBar.BackColor, width);
e.Graphics.DrawRectangle(drawPen, new Rectangle(width / 2, width / 2, this.Width - width, this.Height - width));
drawPen.Dispose();
//Draws the resizer grip
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.Coral, rc);

}
//Handles windows messages
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x84)
{  // Trap WM_NCHITTEST
Point pos = new Point(m.LParam.ToInt32());
pos = this.PointToClient(pos);
//if (pos.Y < cCaption)
//{
//    m.Result = (IntPtr)2;  // HTCAPTION
//    return;
//}
if (
pos.X >= this.ClientSize.Width - cGrip && 
pos.Y >= this.ClientSize.Height - cGrip)
{
m.Result = (IntPtr)17; // HTBOTTOMRIGHT
return;
}
}
base.WndProc(ref m);
}

请注意,我不是一个非常有经验的 C# 程序员。我以前唯一的 C# 经验是使用 Unity 编程,所以如果您能详细解释您可能拥有的解决方案,我将不胜感激。

无论窗体是否无边框,您仍然可以使用MinimumSizeMaximumSize属性。

试试这个:

public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.MinimumSize = new Size(200, 200);
this.MaximumSize = new Size(800, 600);
}

最新更新