Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Save and Restore WPF Window Size, Position, and/or State

, 3 Aug 2012 CPOL
A quick example of how you can save the window size, position, and/or state, and restore your form to that size, position, and state the next time the app is launched.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SaveWindowPosition
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
public Window1()
{
    InitializeComponent();

    var userPrefs = new UserPreferences();

    this.Height = userPrefs.WindowHeight;
    this.Width = userPrefs.WindowWidth;
    this.Top = userPrefs.WindowTop;
    this.Left = userPrefs.WindowLeft;
    this.WindowState = userPrefs.WindowState;

}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    var userPrefs = new UserPreferences();

    userPrefs.WindowHeight = this.Height;
    userPrefs.WindowWidth = this.Width;
    userPrefs.WindowTop=this.Top ;
    userPrefs.WindowLeft = this.Left;
    userPrefs.WindowState = this.WindowState;
    
    userPrefs.Save();
}
        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jeremy Hutchinson
Software Developer Winxnet
United States United States
No Biography provided
Follow on   Twitter   Google+   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 3 Aug 2012
Article Copyright 2010 by Jeremy Hutchinson
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid