51
votes

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)

7
are you calling this.Show() ? or you are changing the state?Akash Kava
@Akash Kava Window.Show only affects the visibility, it has no effect on the window's state. Minimization is a state-issue.H.B.
Well I checked reflector calling Show() does call ShowWindow api as mentioned by others, I wanted to know whether have you tried this.Show() ? because I have used it and may be I have not noticed.Akash Kava
It does not call ShowWindow and it does not work. As i said, it only sets the visibility (and waits for the loaded event to fire).H.B.

7 Answers

93
votes

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.

20
votes

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

15
votes

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,
    }
}
7
votes

For some reason,

WindowState = WindowState.Normal;

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

 Show();
 WindowState = WindowState.Normal;
3
votes

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;
2
votes

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;
}
0
votes

In native Windows you can restore your window to a previous state with ShowWindow(SW_RESTORE):

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.