如何录制窗口位置在Windows窗体应用程序设置

如何录制窗口位置在Windows窗体应用程序设置

本文介绍了如何录制窗口位置在Windows窗体应用程序设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个标准要求:下次用户启动应用程序,在相同的位置和状态,在打开的窗口,因为它以前。这是我的愿望清单:

It seems like a standard requirement: next time the user launches the application, open the window in the same position and state as it was before. Here's my wish list:

  • 在窗口的位置一样,因为它是
  • 除非屏幕已调整和老现在的位置是关闭屏幕。
  • 分配器应保留自己的立场
  • 标签的容器应保持其选择
  • 在一些下拉菜单应该保留他们的选择
  • 在窗口状态(最大化,最小化,正常)是因为它是一样的。
  • 也许你不应该启动最小化,我还没有决定。

我将添加我目前的解决方案,随着限制的答案。

I'll add my current solutions as an answer along with the limitations.

推荐答案

我的另一种选择是写在应用程序中设置多个自定义code和formLoad和formClosed执行它。这不使用数据绑定。

My other option is to write more custom code around the application settings and execute it on formLoad and formClosed. This doesn't use data binding.

缺点:

  • 更多code写的。
  • 非常繁琐。你formLoad设置的属性的顺序混乱。例如,你必须确保你之前设置分离器的距离你已经设置窗口大小。

现在,这是我的preferred解决方案,但它似乎是太辛苦了。为了减少工作,我创建了一个序列化的窗口位置,大小,状态和任何分离器位置,以一个单一的应用程序设置一个WindowSettings类。然后,我可以只创建该类型在我的应用程序中每个表的设置,节省关闭,并在负荷恢复。

Right now, this is my preferred solution, but it seems like too much work. To reduce the work, I created a WindowSettings class that serializes the window location, size, state, and any splitter positions to a single application setting. Then I can just create a setting of that type for each form in my application, save on close, and restore on load.

我贴源$ C ​​$ C ,以及一个示例应用程序包括WindowSettings类和部分使用它的形式。关于将它添加到项目的说明都包括在WindowSettings.cs文件。最棘手的部分是搞清楚如何使用自定义类型添加应用程序设置。您从类型下拉列表中选择浏览...,然后手动输入命名空间和类名。从项目类型没有在列表中显示出来。

I posted the source code, as well as a sample application including the WindowSettings class and some forms that use it. Instructions on adding it to a project are included in the WindowSettings.cs file. The trickiest part was figuring out how to add an application setting with a custom type. You choose Browse... from the type dropdown, and then manually enter the namespace and class name. Types from your project don't show up in the list.

更新:我加了一些静态方法来简化您添加到每个窗体的样板code。一旦你遵循添加WindowSettings类项目,并创建一个应用程序设置的说明,这里的code,它已被添加到每个表单要记录并恢复其地位的一个例子。

Update: I added some static methods to simplify the boilerplate code that you add to each form. Once you've followed the instructions for adding the WindowSettings class to your project and creating an application setting, here's an example of the code that has to be added to each form whose position you want to record and restore.

    private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        Settings.Default.CustomWindowSettings = WindowSettings.Record(
            Settings.Default.CustomWindowSettings,
            this,
            splitContainer1);
    }

    private void MyForm_Load(object sender, EventArgs e)
    {
        WindowSettings.Restore(
            Settings.Default.CustomWindowSettings,
            this,
            splitContainer1);
    }

这篇关于如何录制窗口位置在Windows窗体应用程序设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:27