这是一个如此平凡的问题,我认为我可以立即解决它,但没有。我的表单大小和位置在退出时保存在应用程序设置中,以便下次运行应用程序时恢复。如果用户在最小化表单时关闭表单,我将无法恢复到正常状态。表单恢复为最小化,单击任务栏按钮什么也不做。我在 FormClosing 事件中保存了位置和大小,但如果表单被最小化,我将保存最小化的大小(160、40)和位置(-32000、-32000),这对于恢复表单是完全不正确的。我想强制表单从不恢复最小化,而是恢复到最后的正常大小和位置。不知何故,我必须在表单最小化并保存之前捕获大小和位置,然后在 FormClosing 上,如果表单最小化,则不保存大小和位置。这可能不是100%清楚,
FormClosing 处理程序:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default.WindowLocation = Location;
Settings.Default.WindowSize = Size;
Settings.Default.WindowState = WindowState;
Settings.Default.Save();
}
恢复代码:
private void RestoreWindow()
{
Location = Settings.Default.WindowLocation;
if(Location.X == 0 && Location.Y == 0)
StartPosition = FormStartPosition.CenterScreen;
Size = Settings.Default.WindowSize;
WindowState = FormWindowState.Normal;
if(Size.Width > Screen.PrimaryScreen.WorkingArea.Width)
{
Location = new Point(0, Location.Y);
Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Size.Height);
}
if(Size.Height > Screen.PrimaryScreen.WorkingArea.Height)
{
Location = new Point(Location.X, 0);
Size = new Size(Size.Width, Screen.PrimaryScreen.WorkingArea.Height);
}
}