.NET/Windows窗体:记住窗口大小和位置

cla*_*amp 40 .net c# window winforms

我有一个带有普通窗口的Windows窗体应用程序.现在,当我关闭应用程序并重新启动它时,我希望主窗口出现在屏幕上的同一位置,其大小与关闭时的大小相同.

在Windows窗体中是否有一种简单的方法可以记住屏幕位置和窗口大小(如果可能的话,窗口状态)或者是否必须手动完成所有操作?

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)

它将恢复最后的状态.

它甚至还记得多监视器设置中的哪个监视器最大化了应用程序.

  • 对于那些尚未使用Properties.Settings的人,您需要进入项目属性并转到设置选项卡.创建一个默认设置文件并添加一个名为Location的属性和一个名为Size的属性. (18认同)
  • 您需要在*设置位置后设置WindowState*.否则,窗口将始终在默认监视器上最大化,因为最大化的位置基于您设置WindowState时的位置.这意味着当用户在辅助监视器上最大化它时,它将在主监视器上恢复. (4认同)
  • 好的解决方案。如果您在设置中将 WindowState 保存为 System.Windows.Forms.FormWindowState 类型,则会保存 max 和 min 标志。X (2认同)

Wal*_*ter 34

您需要在应用程序设置中保存窗口位置和大小.这是一篇很好的C#文章,向您展示如何.

编辑

您可以在应用程序设置中保存您想要的任何内容.在设置网格的"类型"列中,您可以浏览到任何.NET类型.WindowState位于System.Windows.Forms中,并列为FormWindowState.FormStartPosition还有一个属性.


jin*_*ing 7

以前的解决方案对我不起作用。玩了一段时间后,我最终得到了以下代码:

  • 保留最大化和正常状态
  • 用默认位置替换最小化状态
  • 如果屏幕尺寸发生变化(分离的显示器、远程连接等),它不会让用户在屏幕外打开应用程序时陷入令人沮丧的状态。

    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 中


dar*_*474 6

我尝试了几种不同的方法;这就是最终为我工作的原因。(在这种情况下 - 首次启动时 - 尚未保留默认值,因此表单将使用设计器中设置的值)

  1. 将设置添加到项目中(手动 - 不要依赖 Visual Studio): 属性.设置

  2. 将以下代码添加到您的表单中:

    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)