0

I am currently busy developing a borderless WinForms form, and I have just gotten some code from here. I modified it to my needs, but I am now stuck. How would I put a limit as to how much it can resize?

Currently, I can resize my window so that it is a mere dot on the screen. How can I put a "cap" on the minimum size?

Here is a snippet of the code used for the resizer(should work as is):

//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);
}

Just as a note, I'm not a very seasoned C# programmer. My only previous experience in C# was programming in Unity, so I would appreciate if you could give me a detailed explanation of what solution you might have.

Vaf Daf
  • 57
  • 1
  • 6

1 Answers1

0

Regardless of the Form being borderless or not, you can still use the MinimumSize and MaximumSize properties.

Try this:

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

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