保存和还原形式位置和大小 [英] Save and Restore Form Position and Size

查看:188
本文介绍了保存和还原形式位置和大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个WinForms 2.0 C#应用程序,哪些是用于保存和恢复申请表的位置和大小的典型方法?

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

详细信息:

我的应用程序是一个现有的Visual FoxPro应用程序的转换。我一直想多读我可以对应用程序设置,用户设置等,并让自己做的事情的净扫清道路,但仍有几件事情我很困惑的。

在福克斯的应用程序,保存的设置存储在注册表中。我的形式子类,我有基类code,它会自动保存表单的位置和大小,在注册表中键入的格式名称。每当我创建一个新的形式,我没有做什么特别的事情来获得这一行为;它的建成在基类​​。我的.Net形式也子类,这部分工作良好。

在.NET中,我得到的IM pression我应该使用用户范围设置像用户preferences。大小和形式的位置肯定看起来像一个用户preference。但是,我看不到任何方式自动添加这些设置项目。换句话说,我每次一个新的窗体添加到我的项目(​​和他们的100的形式),我要记得添加用户范围内的应用程序设置,确保给它的名称相同的形式,即 FormMySpecialSizePosition持有的大小和位置。我宁愿不记得要做到这一点。这只是艰难的运气?还是我完全找错了树,试图使用用户范围设置?我是否需要创建自己的XML文件来保存设置,这样我可以做我想做的事情(比如,在运行时添加一个新的设置)?还是其他什么东西?

当然,这是一个很常见的,有人能告诉正确的方式做到这一点。在此先感谢!

解决方案

 私人无效Form1_Load的(对象发件人,EventArgs的)
{
    //在桌面上还原位置的形式和尺寸
    this.DesktopBounds =
        新的矩形(Properties.Settings.Default.Location,
    Properties.Settings.Default.Size);
    //恢复窗体的窗口状态
    this.WindowState =(FormWindowState)Enum.Parse(
        typeof运算(FormWindowState)
        Properties.Settings.Default.WindowState);
}

私人无效Form1_FormClosing(对象发件人,FormClosingEventArgs E)
{
    System.Drawing.Rectangle边界= 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);
    //坚持形式的位置,大小和窗口状态在桌面上
    Properties.Settings.Default.Save();
}
 

In a WinForms 2.0 C# application, what is the typical method used for saving and restoring form position and size in an application?

Related, is it possible to add new User scoped application settings AT RUNTIME? I totally see how to add settings at design time, that's not a problem. But what if I want to create one at runtime?

More details:

My application is a conversion of an existing Visual FoxPro application. I've been trying to read as much as I can about application settings, user settings, etc. and get myself clear on the .Net way of doing things, but there are still several things I am confused on.

In the Fox app, saved settings are stored in the registry. My forms are subclassed, and I have base class code that automatically saves the form position and size in the registry keyed on the form name. Whenever I create a new form, I don't have to do anything special to get this behavior; it's built in to the base class. My .Net forms are also subclassed, that part is working well.

In .Net, I get the impression I'm supposed to use User scoped settings for things like user preferences. Size and location of a form definitely seem like a user preference. But, I can't see any way to automatically add these settings to the project. In other words, every time I add a new form to my project (and their are 100's of forms), I have to remember to ADD a User scoped application setting and be sure to give it the same name as the form, i.e., "FormMySpecialSizePosition" to hold the size and position. I'd rather not have to remember to do that. Is this just tough luck? Or am I totally barking up the wrong tree by trying to use User scoped settings? Do I need to create my own XML file to hold settings, so that I can do whatever I want (i.e, add a new setting at runtime)? Or something else?

Surely this is a very common and somebody can tell the "right" way to do it. Thanks in advance!

解决方案

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();
}

这篇关于保存和还原形式位置和大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆