保存和还原表单的位置和大小

时间:2020-03-06 14:21:18  来源:igfitidea点击:

在WinForms 2.0 C应用程序中,用于在应用程序中保存和还原表单位置和大小的典型方法是什么?

相关,是否可以在运行时添加新的用户范围的应用程序设置?我完全知道如何在设计时添加设置,这不是问题。但是,如果我想在运行时创建一个呢?

更多细节:

我的应用程序是现有Visual FoxPro应用程序的转换。我一直在尝试尽可能多地阅读有关应用程序设置,用户设置等的内容,使自己对.Net的处理方式有所了解,但是仍然有些事情让我感到困惑。

在Fox应用程序中,保存的设置存储在注册表中。我的表单是子类的,并且我有基类代码,可以自动将表单的位置和大小保存在键入表单名称的注册表中。每当我创建一个新表单时,我都不需要做任何特殊的事情就可以得到这种行为。它内置于基类中。我的.Net表单也被子类化,该部分运行良好。

在.Net中,我得到的印象是应该将用户范围的设置用于诸如用户首选项之类的事情。表单的大小和位置绝对看起来像是用户的首选项。但是,我看不到任何将这些设置自动添加到项目中的方法。换句话说,每次我向项目中添加一个新表单(它们是100个表单)时,我都必须记住要添加一个用户范围的应用程序设置,并确保为其赋予与表单相同的名称,即" FormMySpecialSizePosition"保存大小和位置。我宁愿不必记住这样做。这只是艰难的运气吗?还是我试图通过使用用户范围的设置来完全树错了树?是否需要创建自己的XML文件来保存设置,以便我可以做我想做的任何事情(即在运行时添加新设置)?或者是其他东西?

当然,这是非常普遍的事情,有人可以说出"正确"的做法。提前致谢!

解决方案

我们可以创建具有通用功能的基本表单类,例如记住位置和大小并从该基本类继承。

public class myForm : Form {
protected override void OnLoad(){
    //load the settings and apply them
    base.OnLoad();
}

protected override void OnClose(){
    //save the settings
    base.OnClose();
}
}
then for the other forms:

public class frmMainScreen : myForm {
// you get the settings for free ;)
}

好吧,类似的东西;)

我和我们在同一条船上,因为我要保留许多表格(以我的情况为例,MDI子代),我想保留每个用户的位置和大小。根据我的研究,不支持在运行时创建应用程序设置。 (请参阅此博客条目)
但是,我们不必将所有内容都粘贴在主设置文件中。我们可以将设置文件添加到项目中(在MSDN中进行了说明),并可以通过Properties.Settings对象使用它。这不会减轻必须记住为每种表单创建新的settigns的痛苦,但是至少它将使它们保持在一起,并且不会弄乱主要应用程序设置。

至于使用基类来检索设置……我不知道我们是否可以在那里进行。我将(可能会)做的是为每个属性命名,然后使用Me.GetType()。ToString()(我在VB中工作)来组合要在Load()事件中检索的属性名称每种形式。

private void Form1_Load( object sender, EventArgs e )
{
    // restore location and size of the form on the desktop
    this.DesktopBounds =
        new Rectangle(Properties.Settings.Default.Location,
    Properties.Settings.Default.Size);
    // restore form's window state
    this.WindowState = ( FormWindowState )Enum.Parse(
        typeof(FormWindowState),
        Properties.Settings.Default.WindowState);
}

private void Form1_FormClosing( object sender, FormClosingEventArgs e )
{
    System.Drawing.Rectangle bounds = this.WindowState != FormWindowState.Normal ? this.RestoreBounds : this.DesktopBounds;
    Properties.Settings.Default.Location = bounds.Location;
    Properties.Settings.Default.Size = bounds.Size;
    Properties.Settings.Default.WindowState =
        Enum.GetName(typeof(FormWindowState), this.WindowState);
    // persist location ,size and window state of the form on the desktop
    Properties.Settings.Default.Save();
}

我只是快速又肮脏地将其流式传输到一个单独的xml文件中,可能不是我们之后的内容:

Dim winRect As String() = util.ConfigFile.GetUserConfigInstance().GetValue("appWindow.rect").Split(",")
        Dim winState As String = util.ConfigFile.GetUserConfigInstance().GetValue("appWindow.state")
        '
        Me.WindowState = FormWindowState.Normal
        '
        Me.Left = CType(winRect(0), Integer)
        Me.Top = CType(winRect(1), Integer)
        Me.Width = CType(winRect(2), Integer)
        Me.Height = CType(winRect(3), Integer)
        '
        If winState = "maximised" Then
            Me.WindowState = FormWindowState.Maximized
        End If

Dim winState As String = "normal"
        If Me.WindowState = FormWindowState.Maximized Then
            winState = "maximised"
        ElseIf Me.WindowState = FormWindowState.Minimized Then
            winState = "minimised"
        End If
        '
        If Me.WindowState = FormWindowState.Normal Then
            '
            Dim winRect As String = CType(Me.Left, String) & "," & CType(Me.Top, String) & "," & CType(Me.Width, String) & "," & CType(Me.Height, String)
            ' only save window rectangle if its not maximised/minimised
            util.ConfigFile.GetUserConfigInstance().SetValue("appWindow.rect", winRect)
        End If
        '
        util.ConfigFile.GetUserConfigInstance().SetValue("appWindow.state", winState)

以下是一些相关的签出链接:

使用"应用程序设置"功能保存表单的大小和位置

有关如何使用"应用程序"设置的任何好的示例

探索持久性应用程序设置的秘密