Search code examples
c#winformsresizewndprocmaximize

C# Tell If Form Is Maximising


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 :)


Solution

  • 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