I want to cancel the natural minimizing behavior and change a WPF form size instead.
I have a solution with the Window_StateChanged but it doesn’t look so nice – the window first minimized then jumps back and does the size alteration. Is there a way to accomplish this? I Googled for Window_StateChanging but couldn’t figure it out, some sort of external lib that I prefer not to use.
That’s what I have:
private void Window_StateChanged(object sender, EventArgs e)
{
switch (this.WindowState)
{
case WindowState.Minimized:
{
WindowState = System.Windows.WindowState.Normal;
this.Width = 500;
this.Height = 800;
break;
}
}
}
Thanks,
EP
You’ll need to intercept the minimize command before your form fires
Window_StateChanged, to avoid the minimize/restore dance you’re seeing. I believe the easiest way to do this is to have your form listen for Windows messages and when the minimize command is received, cancel it and resize your form.Register the
SourceInitializedevent, in your form constructor:Add these two handlers to your form:
I suspect this is the approach you were hoping to avoid but boiled down to the code I show it’s not too difficult to implement.
Code based on this SO question.