wpf 如何在代码隐藏中恢复最小化的窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5531548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to restore a minimized Window in code-behind?
提问by H.B.
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.
这是一个有点平凡的问题,但在我看来,WPF 中没有内置的方法。似乎只有WindowState
作为枚举没有帮助的属性,因为我无法判断窗口在最小化之前是否处于Normal
或Maximized
状态。
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)
(我将发布我当前的解决方案作为答案)
回答by Eric Liprandi
Not sure this will work for everybody, but I ran into this today and someoneon 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.Normala second time will "restore" your window to its non-maximized state.
这工作得很好,如果需要,将窗口恢复到最大化。首先检查最小化状态似乎很重要,因为第二次调用WindowState.Normal会将您的窗口“恢复”到其非最大化状态。
Hope this helps.
希望这可以帮助。
回答by Rick Sladkey
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:
WPF 的观点是,这是一个 OS 功能。如果你想弄乱操作系统功能,你可能不得不弄脏你的手。幸运的是,他们为我们提供了这样做的工具。下面是一个 UN-minimize 方法,它采用 WPF 窗口,使用 WIN32 完成效果,无需记录任何状态:
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,
}
}
回答by kiran
SystemCommandsclass has a static method called RestoreWindowthat restores the window to previous state.
SystemCommands类有一个名为RestoreWindow的静态方法,可将窗口恢复到以前的状态。
SystemCommands.RestoreWindow(this); // this being the current window
[Note : SystemCommandsclass 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)]
[注意:SystemCommands类是 .NET 4.5+ (MSDN Ref) 的一部分,用于针对早期版本 Framework 的项目可以使用 WPF Shell 扩展(MSDN Ref)]
回答by Dark Knight
For some reason,
因为某些原因,
WindowState = WindowState.Normal;
didn't work for me. So I used following code & it worked..
对我不起作用。所以我使用了以下代码 & 它工作..
Show();
WindowState = WindowState.Normal;
回答by H.B.
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
这是我现在恢复它的方法:我处理StateChanged
事件以跟踪最后一个状态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:
要恢复,我必须WindowState
分别设置:
this.WindowState = _lastNonMinimizedState;
回答by Heliac
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:
嗯,接受的答案对我不起作用。“最大化”窗口在从任务栏调用时最终会在屏幕上居中(以正常大小显示,即使其状态为最大化),并且诸如通过标题栏拖动窗口之类的操作最终不起作用。最终(几乎通过反复试验),我想出了如何去做。感谢@HB 和@Eric Liprandi 指导我找到答案!代码如下:
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;
}
回答by Alexey Ivanov
In native Windows you can restore your window to a previous state with ShowWindow(SW_RESTORE)
:
在本机 Windows 中,您可以使用以下命令将窗口恢复到以前的状态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.
肯定有 .Net 对应物。