How to restore a minimized Window in code-behind?

WpfWindowStateRestoreMinimize

Wpf Problem Overview


This is somewhat of a mundane question but it seems to me there is no in-built method for it in WPF. There only seems to be the WindowState property which being an enum does not help since i cannot tell whether the Window was in the Normal or Maximized state before being minimized.

When clicking the taskbar icon the window is being restored just as expected, assuming its prior state, but i cannot seem to find any defined method which does that.

So i have been wondering if i am just missing something or if i need to use some custom interaction logic.

(I'll post my current solution as answer)

Wpf Solutions


Solution 1 - Wpf

Not sure this will work for everybody, but I ran into this today and someone on the team suggested "have you tried Normal"?

Turns out he was right. The following seems to nicely restore your window:

if (myWindow.WindowState == WindowState.Minimized)
    myWindow.WindowState = WindowState.Normal;

That works just fine, restoring the window to Maximized if needed. It seems critical to check for the minimized state first as calling WindowState.Normal a second time will "restore" your window to its non-maximized state.

Hope this helps.

Solution 2 - Wpf

SystemCommands class has a static method called RestoreWindow that restores the window to previous state.

SystemCommands.RestoreWindow(this); // this being the current window

[Note : SystemCommands class is part of .NET 4.5+ (MSDN Ref) for projects that target to earlier versions of Framework can use the WPF Shell extension (MSDN Ref)]

Solution 3 - Wpf

WPF's point of view is that this is an OS feature. If you want to mess around with OS features you might have to get your hands dirty. Luckily they have provided us with the tools to do so. Here is a UN-minimize method that takes a WPF window and uses WIN32 to accomplish the effect without recording any state:

public static class Win32
{
    public static void Unminimize(Window window)
    {
        var hwnd = (HwndSource.FromVisual(window) as HwndSource).Handle;
        ShowWindow(hwnd, ShowWindowCommands.Restore);
    }

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

    private enum ShowWindowCommands : int
    {
        /// <summary>
        /// Activates and displays the window. If the window is minimized or 
        /// maximized, the system restores it to its original size and position. 
        /// An application should specify this flag when restoring a minimized window.
        /// </summary>
        Restore = 9,
    }
}

Solution 4 - Wpf

For some reason,

WindowState = WindowState.Normal;

didn't work for me. So I used following code & it worked..

 Show();
 WindowState = WindowState.Normal;

Solution 5 - Wpf

Here is how i get it to restore right now: I handle the StateChanged event to keep track of the last state that was not Minimized

WindowState _lastNonMinimizedState = WindowState.Maximized;
private void Window_StateChanged(object sender, EventArgs e)
{
	if (this.WindowState != System.Windows.WindowState.Minimized)
	{
		_lastNonMinimizedState = WindowState;
	}
}

To restore i then have to set that WindowState respectively:

this.WindowState = _lastNonMinimizedState;

Solution 6 - Wpf

Hmmm, the accepted answer did not work for me. The "maximized" window, when recalled from the task bar would end up centering itself (displaying in its Normal size, even though its state is Maximized) on the screen and things like dragging the window by its title bar ended up not working. Eventually (pretty much by trial-and-error), I figured out how to do it. Thanks to @H.B. and @Eric Liprandi for guiding me to the answer! Code follows:

private bool windowIsMinimized = false;
private WindowState lastNonMinimizedState = WindowState.Normal;

private void Window_StateChanged(object sender, EventArgs e)
{
    if (this.windowIsMinimized)
    {
        this.windowIsMinimized = false;
        this.WindowState = WindowState.Normal;
        this.WindowState = this.lastNonMinimizedState;
    }
    else if (this.WindowState == WindowState.Minimized)
    {
        this.windowIsMinimized = true;
    }
}

private void Window_MinimizeButtonClicked(object sender, MouseButtonEventArgs e)
{
    this.lastNonMinimizedState = this.WindowState;
    this.WindowState = WindowState.Minimized;
    this.windowIsMinimized = true;
}

private void Window_MaximizeRestoreButtonClicked(object sender, MouseButtonEventArgs e)
{
    if (this.WindowState == WindowState.Normal)
    {
        this.WindowState = WindowState.Maximized;
    }
    else
    {
        this.WindowState = WindowState.Normal;
    }
    
    this.lastNonMinimizedState = this.WindowState;
}

Solution 7 - Wpf

In native Windows you can restore your window to a previous state with http://msdn.microsoft.com/en-us/library/ms633548.aspx">ShowWindow</a>(SW_RESTORE)</code>;:

> Activates and displays the window. If the window is minimized or maximized, the system restores it to its original size and position. An application should specify this flag when restoring a minimized window.

There's surely .Net counterpart to that.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionH.B.View Question on Stackoverflow
Solution 1 - WpfEric LiprandiView Answer on Stackoverflow
Solution 2 - WpfkiranView Answer on Stackoverflow
Solution 3 - WpfRick SladkeyView Answer on Stackoverflow
Solution 4 - WpfDark KnightView Answer on Stackoverflow
Solution 5 - WpfH.B.View Answer on Stackoverflow
Solution 6 - WpfRiegardt SteynView Answer on Stackoverflow
Solution 7 - WpfAlexey IvanovView Answer on Stackoverflow