euclid135 euclid135 - 17 days ago 7
C# Question

Event called after windows maximized

I'm looking for an event from a form that called after a window is maximized or minimized.

As far as I know there is such event as SizeChanged or WndProc that can handle maximized window, but it's called right away after user try to maximize the window, and it's not called after the window is fully maximized.

I'm looking for event like ResizeEnd, but maybe this one is called MaximizedEnd or MinimizedEnd

Is there anyway to do that?

Answer

This is what Gabriel's solution would look like in detail. I don't think there is an event for WindoStateChanged either.

I just tested this solution out and it is working when you click the maximize button. It appears to be getting fired 3 times though. I would maybe do a little debugging and figure out on exactly what m.Msg you want to intercept to check if the state has changed. I found a quick reference of some of those WM_ messages here http://www.autohotkey.com/docs/misc/SendMessageList.htm.

    protected override void WndProc(ref Message m)
    {
        FormWindowState previousWindowState = this.WindowState;

        base.WndProc(ref m);

        FormWindowState currentWindowState = this.WindowState;

        if (previousWindowState != currentWindowState && currentWindowState == FormWindowState.Maximized)
        {
            // TODO: Do something the window has been maximized

        }
    }

As stated the above code gets fired 3 times at least while I have tested. The below code only gets fired once. It is a bit more lengthy but also may be more intuitive and more fully addresses your question of how to fire an event. Thanks to Yore for in his comment to your question for this idea.

public Form1()
{
    InitializeComponent();

    this.SizeChanged +=new EventHandler(Form1_SizeChanged);
    FormMaximized += new EventHandler(Form1_FormMaximized);

    _CurrentWindowState = this.WindowState;
    if (_CurrentWindowState == FormWindowState.Maximized)
    {
        FireFormMaximized();
    }
}

public event EventHandler FormMaximized;
private void FireFormMaximized()
{
    if (FormMaximized != null)
    {
        FormMaximized(this, EventArgs.Empty);
    }
}

private FormWindowState _CurrentWindowState;
private void Form1_SizeChanged(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Maximized && _CurrentWindowState != FormWindowState.Maximized)
    {
        FireFormMaximized();
    }
    _CurrentWindowState = this.WindowState;
}

void Form1_FormMaximized(object sender, EventArgs e)
{
    //TODO Put you're code here
}