如何将当前最小化的窗体恢复到先前的状态。我找不到任何方法来确定它以前的 WindowState
是 Normal
还是 Maximized
;但我知道信息必须存储在某个地方,因为 Windows 在任务栏上对于应用程序执行此操作没有问题。
这方面没有托管 API。做法是通过 PInvoke GetWindowPlacement 并检查 WPF_RESTORETOMAXIMIZED
。
有关详情,请参见此Microsoft How To(演示了 VB 中的技术)。
在 C# 中,可以这样实现:
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public System.Drawing.Point ptMinPosition;
public System.Drawing.Point ptMaxPosition;
public System.Drawing.Rectangle rcNormalPosition;
}
public void RestoreFromMinimzied(Form form)
{
const int WPF_RESTORETOMAXIMIZED = 0x2;
WINDOWPLACEMENT placement = new WINDOWPLACEMENT();
placement.length = Marshal.SizeOf(placement);
GetWindowPlacement(form.Handle, ref placement);
if ((placement.flags & WPF_RESTORETOMAXIMIZED) == WPF_RESTORETOMAXIMIZED)
form.WindowState = FormWindowState.Maximized;
else
form.WindowState = FormWindowState.Normal;
}
this.WindowState = FormWindowState.Normal;
你还有:
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;
啊,我误解了这个问题:
从最小化状态恢复窗口状态应该是你正在寻找的。它说你可以像这样模拟任务栏的行为:SendMessage(form.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
prevState = currState = this.WindowState;
}
protected override void OnResize(EventArgs e) {
if (currState != this.WindowState) {
prevState = currState;
currState = this.WindowState;
}
base.OnResize(e);
}
private FormWindowState prevState, currState;
}
如果您想在更改窗口状态(最大化/最小化)时存储先前的状态,您需要钩取SizeChanged
事件,根据MSDN上的帖子。您可以在那里获取WindowState
并将其存储。