Chr*_*isF 59
如果将此代码添加到FormClosing事件处理程序:
if (WindowState == FormWindowState.Maximized)
{
Properties.Settings.Default.Location = RestoreBounds.Location;
Properties.Settings.Default.Size = RestoreBounds.Size;
Properties.Settings.Default.Maximised = true;
Properties.Settings.Default.Minimised = false;
}
else if (WindowState == FormWindowState.Normal)
{
Properties.Settings.Default.Location = Location;
Properties.Settings.Default.Size = Size;
Properties.Settings.Default.Maximised = false;
Properties.Settings.Default.Minimised = false;
}
else
{
Properties.Settings.Default.Location = RestoreBounds.Location;
Properties.Settings.Default.Size = RestoreBounds.Size;
Properties.Settings.Default.Maximised = false;
Properties.Settings.Default.Minimised = true;
}
Properties.Settings.Default.Save();
Run Code Online (Sandbox Code Playgroud)
它将保存当前状态.
然后将此代码添加到表单的OnLoad处理程序:
if (Properties.Settings.Default.Maximised)
{
WindowState = FormWindowState.Maximized;
Location = Properties.Settings.Default.Location;
Size = Properties.Settings.Default.Size;
}
else if (Properties.Settings.Default.Minimised)
{
WindowState = FormWindowState.Minimized;
Location = Properties.Settings.Default.Location;
Size = Properties.Settings.Default.Size;
}
else
{
Location = Properties.Settings.Default.Location;
Size = Properties.Settings.Default.Size;
}
Run Code Online (Sandbox Code Playgroud)
它将恢复最后的状态.
它甚至还记得多监视器设置中的哪个监视器最大化了应用程序.
以前的解决方案对我不起作用。玩了一段时间后,我最终得到了以下代码:
如果屏幕尺寸发生变化(分离的显示器、远程连接等),它不会让用户在屏幕外打开应用程序时陷入令人沮丧的状态。
private void MyForm_Load(object sender, EventArgs e)
{
if (Properties.Settings.Default.IsMaximized)
WindowState = FormWindowState.Maximized;
else if (Screen.AllScreens.Any(screen => screen.WorkingArea.IntersectsWith(Properties.Settings.Default.WindowPosition)))
{
StartPosition = FormStartPosition.Manual;
DesktopBounds = Properties.Settings.Default.WindowPosition;
WindowState = FormWindowState.Normal;
}
}
private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.IsMaximized = WindowState == FormWindowState.Maximized;
Properties.Settings.Default.WindowPosition = DesktopBounds;
Properties.Settings.Default.Save();
}
Run Code Online (Sandbox Code Playgroud)用户设置:
<userSettings>
<WindowsFormsApplication2.Properties.Settings>
<setting name="WindowPosition" serializeAs="String">
<value>0, 0, -1, -1</value>
</setting>
<setting name="IsMaximized" serializeAs="String">
<value>False</value>
</setting>
</WindowsFormsApplication2.Properties.Settings>
</userSettings>
Run Code Online (Sandbox Code Playgroud)
注意:WindowsPosition 是故意错误的,所以在第一次启动应用程序时将使用默认位置。
请注意 IntersectsWith 需要一个 Rectangle,而不是一个 Point。所以与其他答案不同,这个答案是将 DesktopBounds 而不是 Location 保存到 Properties.Settings.Default.WindowPosition 中
我尝试了几种不同的方法;这就是最终为我工作的原因。(在这种情况下 - 首次启动时 - 尚未保留默认值,因此表单将使用设计器中设置的值)
将以下代码添加到您的表单中:
private void Form1_Load(object sender, EventArgs e)
{
this.RestoreWindowPosition();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
this.SaveWindowPosition();
}
private void RestoreWindowPosition()
{
if (Settings.Default.HasSetDefaults)
{
this.WindowState = Settings.Default.WindowState;
this.Location = Settings.Default.Location;
this.Size = Settings.Default.Size;
}
}
private void SaveWindowPosition()
{
Settings.Default.WindowState = this.WindowState;
if (this.WindowState == FormWindowState.Normal)
{
Settings.Default.Location = this.Location;
Settings.Default.Size = this.Size;
}
else
{
Settings.Default.Location = this.RestoreBounds.Location;
Settings.Default.Size = this.RestoreBounds.Size;
}
Settings.Default.HasSetDefaults = true;
Settings.Default.Save();
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
45406 次 |
| 最近记录: |