How to save and use application window size?

Using .NET 4, what is the best way to keep the size of the application window and its position when closing and use these values ​​to launch the application window the next time it starts?

I prefer not to touch any registry, but I don’t know if there is any app.config (similar to web.config for ASP.NET applications) that I can use for Windows Presentation Foundation applications.

Thanks.

+4
source share
3 answers

Description

Windows forms

  • Creating properties in the application settings LocationX, LocationY, WindowWidth, WindowHeight (type int)
  • Save location and size in Form_FormClosed
  • Load and apply layout and size in Form_Load

Example

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

Additional Information

WPF

  • Creating properties in the application settings LocationX, LocationY, WindowWidth, WindowHeight (type double)
  • Save location and size in MainWindow_Closed
  • Load and apply layout and size in MainWindow_Loaded

Example

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

Additional Information

I tested both WinForms and WPF.

+9
source

If you are going to save only one position and size window, I would suggest saving them in applicationSettings .

If you have more window settings for saving or more windows for management, I personally would suggest saving it in a separate XML file.

EDIT

Working with a standard XML example

LINQ to XML Example

Hope this helps.

+1
source

I know that they have answered about this for a long time, but this is the most elegant solution that I found on the Internet after two days of searching for a worthy solution. Check this:

http://blogs.msdn.com/b/davidrickard/archive/2010/03/09/saving-window-size-and-location-in-wpf-and-winforms.aspx

It also works for WPF and for WinForms.

0
source

Source: https://habr.com/ru/post/1389568/


All Articles