2

Ok heres my problem. I have a form that when it is not maximised, its maximum size has to be the total height of the components inside the form. To achieve this, i use this:

    private void resize_form(object sender, EventArgs e)
    {
        this.MaximumSize = new System.Drawing.Size(1000, this.panel4.Height + this.label2.Height + this.HeightMin);
    }

That fires on the Resize event of the form. Because the component size is always changing it made sense to do this on a resize event. How ever if i want to maximise the form, the form just goes to the highest settings defined in this.MaximumSize. So i was wondering is there a way to tell when a form is going to be maximised and set its maximumsize to the screen boundarys before the form maximises.

If there is a better way to change the maximumsize value without resize event, that would also be great :)

Ozzy
  • 10,285
  • 26
  • 94
  • 138

5 Answers5

9

You still need to use the resize event, but check the WindowState:

if (this.WindowState == FormWindowState.Maximized)
{
    // Do your stuff
}

As yshuditelu points out you can set the minimum size property of your form too - which should, when coupled with judicious use of anchor values, mean that it can never shrink too far and when it does grow the components will move and/or grow as required.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • Already tried that. The proplem is if i try to edit the width and height of the form when its maximised, nothing happens. Even if i edit the MaximumSize property – Ozzy Jun 02 '09 at 16:43
  • I think by setting the MaximumSize property you have effectively disabled the Maximize functionality. Try setting the MaximumSize to (0,0) if the WindowSate is Maximized. – ChrisF Jun 02 '09 at 16:47
  • I've just tried it and when I clicked maximize, my app moved to the top left of the screen and stayed at the maximum size. – ChrisF Jun 02 '09 at 16:52
  • You also don't get the WindowState set to Maximized. – ChrisF Jun 02 '09 at 16:55
3

I found the answer that suited me perfectly. A lil WndProc override :D (i love WndProc now)

protected override void WndProc(ref Message message)
{
    const int WM_SYSCOMMAND = 0x0112;
    const int SC_MAXIMIZE = 0xF030; 

    switch (message.Msg)
    {
        case WM_SYSCOMMAND:
            int command = message.WParam.ToInt32() & 0xfff0;
            if (command == SC_MAXIMIZE) 
            {
                this.maximize = true;
                this.MaximumSize = new System.Drawing.Size(0, 0);
            }
            break;
    }

    base.WndProc(ref message);
}

private void resize_form(object sender, EventArgs e)
{
    if (!maximize)
    {
        this.MaximumSize = new System.Drawing.Size(1000, this.panel4.Height + this.label2.Height + this.HeightMin);
    }
}

Basically it sets this.maximize to true when it receives teh SC_MAXIMIZE message. The resize event will only set a new MaximumSize if this.maximize is set to false. Nifty xD

Ozzy
  • 10,285
  • 26
  • 94
  • 138
2

Are you sure you don't want to be setting the MinimumSize property? If you set the MinimumSize to the size of all the labels, then the form will never be smaller than that. But it can still grow to whatever size the user wants, so long as it is larger than the minimum.

Timothy Carter
  • 15,459
  • 7
  • 44
  • 62
  • I already have a minimum size set. Being a design perfectionist (not saying im good at design tho :P) i cant have the form be larger then all the controls it displays. – Ozzy Jun 02 '09 at 16:42
0

If the user clicks in the upper bar, they can resize the window, so I use this:

private void Form1_Resize(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Normal)
    {
        this.WindowState = FormWindowState.Maximized;
    }
}
dat3450
  • 954
  • 3
  • 13
  • 27
0

Check out the System.Windows.Forms.Screen class. Get the screen from a relevant point (to handle the multi-mon case) and then get its resolution.

This should work in conjunction with the other comment about checking FormWindowState.Maximized.

Drew Hoskins
  • 4,168
  • 20
  • 23