使用 .NET 4,在关闭时保存应用程序的窗口大小和位置并在下次运行时使用这些值启动应用程序的窗口的最佳方法是什么?
我不想接触任何类型的注册表,但不知道是否有某种 app.config(类似于 ASP.NET 应用程序的 web.config)可以用于 Windows Presentation Foundation 应用程序。
Thanks.
描述
Windows 窗体
- 在应用程序设置中创建属性地点X, 地点Y, 窗宽, 窗口高度(类型int)
- 保存位置和大小
Form_FormClosed
- 加载并应用位置和尺寸
Form_Load
Sample
private void Form1_Load(object sender, EventArgs e)
{
this.Location = new Point(Properties.Settings.Default.LocationX, Properties.Settings.Default.LocationY);
this.Width = Properties.Settings.Default.WindowWidth;
this.Height = Properties.Settings.Default.WindowHeight;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Properties.Settings.Default.LocationX = this.Location.X;
Properties.Settings.Default.LocationY = this.Location.Y;
Properties.Settings.Default.WindowWidth = this.Width;
Properties.Settings.Default.WindowHeight = this.Height;
Properties.Settings.Default.Save();
}
更多信息
-
Windows 窗体的应用程序设置 http://msdn.microsoft.com/en-us/library/0zszyc6e.aspx
- MSDN - Form.Load 事件 http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load.aspx
- MSDN - Form.Closed 事件 http://msdn.microsoft.com/en-us/library/system.windows.forms.form.closed.aspx
WPF
- 在应用程序设置中创建属性地点X, 地点Y, 窗宽, 窗口高度(类型double)
- 保存位置和大小
MainWindow_Closed
- 加载并应用位置和尺寸
MainWindow_Loaded
Sample
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
this.Left = Properties.Settings.Default.LocationX;
this.Top = Properties.Settings.Default.LocationY;
this.Width = Properties.Settings.Default.WindowWidth;
this.Height = Properties.Settings.Default.WindowHeight;
}
void MainWindow_Closed(object sender, EventArgs e)
{
Properties.Settings.Default.LocationX = this.Left;
Properties.Settings.Default.LocationY = this.Top;
Properties.Settings.Default.WindowWidth = this.Width;
Properties.Settings.Default.WindowHeight = this.Height;
Properties.Settings.Default.Save();
}
更多信息
-
应用程序设置 http://msdn.microsoft.com/en-us/library/0zszyc6e.aspx
- MSDN - FrameworkElement.Loaded 事件 http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.loaded%28v=vs.95%29.aspx
- MSDN - Window.Closed 事件 http://msdn.microsoft.com/en-us/library/system.windows.window.closed.aspx
我已经测试了 WinForms 和 WPF。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)