保存和恢复表单位置和大小 [英] Save and Restore Form Position and Size

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

问题描述

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

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?

更多详情:

我的应用程序是对现有 Visual FoxPro 应用程序的转换.我一直在尝试尽可能多地阅读有关应用程序设置、用户设置等的信息,并让自己清楚 .Net 的做事方式,但仍有几件事让我感到困惑.

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.

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

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.

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

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天全站免登陆