这是一个有点平凡的问题,但在我看来,WPF 中没有内置的方法。似乎只有WindowState
作为枚举的属性没有帮助,因为我无法判断窗口在被最小化之前是否处于Normal
or状态。Maximized
单击任务栏图标时,窗口正在按预期恢复,假设它的先前状态,但我似乎找不到任何定义的方法来做到这一点。
所以我一直想知道我是否只是遗漏了一些东西,或者我是否需要使用一些自定义交互逻辑。
(我将发布我当前的解决方案作为答案)
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.
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)]
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,
}
}
For some reason,
WindowState = WindowState.Normal;
didn't work for me. So I used following code & it worked..
Show();
WindowState = WindowState.Normal;
这是我现在恢复它的方法:我处理StateChanged
事件以跟踪最后一个不是的状态Minimized
WindowState _lastNonMinimizedState = WindowState.Maximized;
private void Window_StateChanged(object sender, EventArgs e)
{
if (this.WindowState != System.Windows.WindowState.Minimized)
{
_lastNonMinimizedState = WindowState;
}
}
要恢复我必须WindowState
分别设置:
this.WindowState = _lastNonMinimizedState;
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;
}
在本机 Windows 中,您可以使用以下命令将窗口恢复到以前的状态ShowWindow(SW_RESTORE)
:
激活并显示窗口。如果窗口被最小化或最大化,系统会将其恢复到原来的大小和位置。应用程序在恢复最小化窗口时应指定此标志。
肯定有 .Net 与之对应。