如何获取当前最小化的表单并将其还原到以前的状态。我找不到任何方法来确定其先前的WindowState
是Normal
还是Maximized
;但是我知道信息必须存储在某个地方,因为Windows在任务栏上使用应用程序时没有问题。
答案 0 :(得分:9)
此处没有托管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;
}
答案 1 :(得分:7)
this.WindowState = FormWindowState.Normal;
您还有:
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Maximized;
啊,我误解了这个问题:
Restore WindowState from Minimized应该是您正在寻找的。它说你可以像这样模仿任务栏的行为:
SendMessage(form.Handle, WM_SYSCOMMAND, SC_RESTORE, 0);
答案 2 :(得分:3)
您可以通过Resize事件跟踪窗口状态更改。像这样:
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;
}
答案 3 :(得分:1)
如果您想在存在更改(最大化/最小化)时存储先前的状态,则必须根据this post on MSDN挂钩SizeChanged
事件。您可以在那里获取WindowState
并存储它。