防止自定义边框调整大小句柄将 winform 调整得太小

Preventing a custom border resize handle from resizing a winform too small

我目前正忙于开发一个无边界的 WinForms 窗体,我刚刚从 here 那里得到了一些代码。我根据需要修改了它,但现在卡住了。我如何限制它可以调整多少?

目前,我可以调整 window 的大小,使其在屏幕上只是一个点。如何在最小尺寸上加上 "cap"?

这是用于缩放器的代码片段(应该按原样工作):

//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 中编程,所以如果你能详细解释一下你可能有什么解决方案,我将不胜感激。

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

试试这个:

public Form1()
{
    InitializeComponent();
    this.DoubleBuffered = true;
    this.SetStyle(ControlStyles.ResizeRedraw, true);

    this.MinimumSize = new Size(200, 200);
    this.MaximumSize = new Size(800, 600);
}