Click here to Skip to main content
Click here to Skip to main content

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

, 3 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

Introduction

A quick example of how you can save a window's size, position, and/or state, and restore to that size, position, and state the next time the app is launched.

Background

This started as part of a real world app where I wanted the user to be able to resize the window and not have the window return to the default size the next time the app was launched. Many of my users have two monitors, so I decided to let them choose which monitor to open the window on, but I also wanted to make sure it didn't open on their "second monitor" when they took their laptop home and had only one monitor.

Using the Code

The first thing I did was setup some user settings to hold the data I wanted to save.

Then, I created a UserPreferences class to handle loading and saving of the settings. The class consists of a property (with backing variable) for each of the settings, and a few methods for loading, saving, and manipulating the settings.

Here are the methods for loading and saving the settings. Notice that when I save the settings, I don't save if the window state is minimized. I don't want my app to start minimized.

private void Load()
{
    _windowTop = Properties.Settings.Default.WindowTop;
    _windowLeft = Properties.Settings.Default.WindowLeft;
    _windowHeight = Properties.Settings.Default.WindowHeight;
    _windowWidth = Properties.Settings.Default.WindowWidth;
    _windowState = Properties.Settings.Default.WindowState;
}

public void Save()
{
    if (_windowState != System.Windows.WindowState.Minimized)
    {
        Properties.Settings.Default.WindowTop = _windowTop;
        Properties.Settings.Default.WindowLeft = _windowLeft;
        Properties.Settings.Default.WindowHeight = _windowHeight;
        Properties.Settings.Default.WindowWidth = _windowWidth;
        Properties.Settings.Default.WindowState = _windowState;

        Properties.Settings.Default.Save();
    }
}

I then created a method to shrink the window down to fit in the current desktop, if needed.

public void SizeToFit()
{
    if (_windowHeight > System.Windows.SystemParameters.VirtualScreenHeight)
    {
        _windowHeight = System.Windows.SystemParameters.VirtualScreenHeight;
    }

    if (_windowWidth > System.Windows.SystemParameters.VirtualScreenWidth)
    {
        _windowWidth = System.Windows.SystemParameters.VirtualScreenWidth;
    }
}

And lastly, I created a method that moves the window onto the desktop if it is more than half out of view. This is really important if you are going to save the position, because we don't want to restore the window to a position off the user's desktop.

public void MoveIntoView()
{
    if (_windowTop + _windowHeight / 2 > 
         System.Windows.SystemParameters.VirtualScreenHeight)
    {
        _windowTop = 
          System.Windows.SystemParameters.VirtualScreenHeight - 
          _windowHeight;
    }

    if (_windowLeft + _windowWidth / 2 > 
             System.Windows.SystemParameters.VirtualScreenWidth)
    {
        _windowLeft = 
          System.Windows.SystemParameters.VirtualScreenWidth - 
          _windowWidth;
    }

    if (_windowTop < 0)
    {
        _windowTop = 0;
    } 
    
    if (_windowLeft < 0)
    {
        _windowLeft = 0;
    }
}

The constructor for the UserPreferences class calls Load(), SizeToFit(), and MoveIntoView().

public UserPreferences()
{
    //Load the settings
    Load();

    //Size it to fit the current screen
    SizeToFit();

    //Move the window at least partially into view
    MoveIntoView();
}

Then, in the window you want to resize, we just load the settings into the window in the constructor, and save the settings on Window_Closing.

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

Points of Interest

When I resized and repositioned my form, I didn't take into account the height of the Windows task bar, so if the user resizes the screen to a smaller screen, the bottom of my form may be hidden by the task bar.

History

  • 1/7/09 - Initial post.
  • 1/8/09 - Removed binding from window to UserPreferences to allow setting the window size at design time and to prevent binding from being removed if you resize the window using a designer.

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

Comments and Discussions

 
QuestionMoveIntoView isn't quite correct PinmemberDougPotter16-Oct-13 14:25 
GeneralThis Solution *way* better than Josh Smith's version - Vote of 5 PinmemberBCantor16-Jul-13 7:38 
GeneralRe: This Solution *way* better than Josh Smith's version - Vote of 5 PinmemberBCantor16-Jul-13 7:41 
GeneralMy vote of 4 PinmemberChristian Amado3-Aug-12 6:29 
BugMistype? PinmemberAlexP1122328-Jul-12 9:05 
GeneralRe: Mistype? PinmemberJeremy Hutchinson3-Aug-12 7:02 
QuestionMy vote of 5 too PinmemberKarl Shifflett29-May-12 7:05 
GeneralMy vote of 5 PinmemberKarl Shifflett29-May-12 7:04 
GeneralMy vote of 5 Pinmembergpasupathi23-Aug-11 6:21 
Excellent articale, thanka a lot
QuestionAny chance of an update for it to work in an MVVM scenario? Pinmembergeoffhirst28-Jul-11 3:12 
AnswerRe: Any chance of an update for it to work in an MVVM scenario? PinmemberJeremy Hutchinson28-Jul-11 3:17 
AnswerRe: Any chance of an update for it to work in an MVVM scenario? PinmemberKenny Woo9-Apr-12 19:06 
AnswerRe: Any chance of an update for it to work in an MVVM scenario? PinmemberErik Vullings28-Sep-12 5:19 
GeneralGreat article, how would you extend it to multiple forms PinmemberMember 456012118-Mar-11 20:53 
GeneralRe: Great article, how would you extend it to multiple forms PinmemberJeremy Hutchinson21-Mar-11 3:33 
GeneralBug with some dual screen configs.... PinmemberYbbozman6-Sep-10 23:44 
GeneralBehavior less than optimal on dual screen setup PinmemberDan Neely25-Jan-10 6:55 
GeneralRe: Behavior less than optimal on dual screen setup PinmemberJeremy Hutchinson25-Jan-10 7:06 
GeneralRe: Behavior less than optimal on dual screen setup Pinmemberstgn20-Jul-10 4:23 
GeneralNIce idea, but I'm afraid its not a new one PinmvpSacha Barber9-Jan-10 0:28 
GeneralRe: NIce idea, but I'm afraid its not a new one PinmemberBillWoodruff9-Jan-10 0:59 
GeneralRe: NIce idea, but I'm afraid its not a new one PinmvpSacha Barber9-Jan-10 1:08 
GeneralRe: NIce idea, but I'm afraid its not a new one PinmemberJeremy Hutchinson9-Jan-10 4:01 
GeneralRe: NIce idea, but I'm afraid its not a new one PinmvpSacha Barber9-Jan-10 6:04 
GeneralIt's a better idea. PinmemberMember 740954121-Oct-10 16:11 
GeneralRe: NIce idea, but I'm afraid its not a new one PinmemberBCantor16-Jul-13 7:28 
GeneralRe: NIce idea, but I'm afraid its not a new one PinmvpSacha Barber16-Jul-13 8:33 
Generalthanks, and one question PinmemberBillWoodruff8-Jan-10 20:14 
GeneralRe: thanks, and one question PinmemberJeremy Hutchinson9-Jan-10 4:02 
GeneralThanks for the time and effort of writing this article Pinmembercyberdyme8-Jan-10 0:44 
GeneralMy vote of 1 Pinmemberhaadikhan27-Jan-10 9:52 
GeneralRe: My vote of 1 PinmvpPete O'Hanlon7-Jan-10 10:21 
GeneralRe: My vote of 1 PinmemberJeremy Hutchinson7-Jan-10 10:43 
GeneralRe: My vote of 1 PinmemberTonyJ7-Jan-10 11:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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