Restore window location in WPF

For years I was server-oriented developer, quite often writing code for Unix environment and helping others to do so. However I never mind doing occasional user-oriented application or two. Lately I've been doing a lot of .NET GUI development using WPF (Windows Presentation Framework). A small task seems to come up quite often. If you have a GUI application, you usually want it to start in the same state it was left it when used last time. In particular this is true regarding its position and size. Effectively, you would like your application to restore its position ans size automatically on startup. So the following procedure should take care of that.

First, add new settings to your project, by right-clicking your project name, selecting properties and then going into "Settings" tab:

Name Type Default value
WindowHeight double 0
WindowWidth double 0
WindowLocationX double 0
WindowLocationY double 0
WindowState string ""

These properties will store the windows state data between the runs. .NET automated settings API handles all the details for us behind the senses.

Next, this these three lines must be present in your constructor:

  1.         public MainWindow()
  2.         {
  3.             Properties.Settings.Default.Upgrade();  // ensures proper migration of settings between versions
  4.             InitializeComponent();
  5.             _RestoreWindow();
  6.             this.Closing += mainWindow_Closing;   // register our event handler, can also be done in XAML
  7.         }

And these two functions must be further defined in your implementation of MainWindow:

  1.         private void _RestoreWindow()
  2.         {
  3.             if (Properties.Settings.Default.WindowHeight == 0
  4.                 || Properties.Settings.Default.WindowWidth == 0)
  5.             {
  6.                 return;
  7.             }
  8.             this.Height = Properties.Settings.Default.WindowHeight;
  9.             this.Width = Properties.Settings.Default.WindowWidth;
  10.             this.Left = Properties.Settings.Default.WindowLocationX;
  11.             this.Top = Properties.Settings.Default.WindowLocationY;
  12.             if (Properties.Settings.Default.WindowState == "Normal")
  13.             {
  14.                 this.WindowState = WindowState.Normal;
  15.             }
  16.             else if (Properties.Settings.Default.WindowState == "Maximized")
  17.             {
  18.                 this.WindowState = WindowState.Maximized;
  19.             }
  20.         }
  21.  
  22.         private void mainWindow_Closing(object sender, CancelEventArgs e)
  23.         {
  24.             Properties.Settings.Default.WindowWidth = this.Width;
  25.             Properties.Settings.Default.WindowHeight = this.Height;
  26.             Properties.Settings.Default.WindowLocationX = this.Left;
  27.             Properties.Settings.Default.WindowLocationY = this.Top;
  28.             Properties.Settings.Default.WindowState = Convert.ToString(this.WindowState,
  29.                 CultureInfo.CurrentCulture);
  30.             Properties.Settings.Default.Save(); // ensure that your settings are written to the disk
  31.         } 

That's it - job done!

Topic: 

Add new comment