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

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

By , 3 Aug 2012
 

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)

About the Author

Jeremy Hutchinson
Software Developer Winxnet
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralGreat article, how would you extend it to multiple formsmemberMember 456012118 Mar '11 - 19:53 
Thanks for writing this article, it provides a good solution to a problem I have been trying to solve for while. My solution involved created an "ini" file which allowed me to store settings for multiple forms. Is there a way to extend this solution to allow different forms to save their position information separately? Thanks in advance.
 

Donovan
GeneralRe: Great article, how would you extend it to multiple formsmemberJeremy Hutchinson21 Mar '11 - 2:33 
Good question, I should try to find some time to revisit and update this article to allow saving multiple windows. There's two approaches that come to mind:
 
1. Turn the UserPreferences class into an abstract class, and change the Load and Save methods into Abstract methods. Then inherit from it in classes named like Window1UserPreferences, and implement the Load and Save methods to use Settings named Window1Top, Window1Left etc.
 
2. Create a class to hold all of the window settings and serialize that to UserSettings named Window1Settings, Window2Settings etc.
 
I haven't explored either option yet, so I'm not sure of the hangups of each, but if you have more than 2 or 3 windows you want to save settings for it seems like option 2 would be the better way to go.
 
One more thought I had on this: We could be using System.Drawing.Size and System.Drawing.Point for the Window Size, Top and Left.
GeneralBug with some dual screen configs....memberYbbozman6 Sep '10 - 22:44 
Hi,
 
Great job Jeremy Hutchinson. But small bug!
The function MoveIntoView doesn't work properly in some dual-screen config. In my case, the right screen is Screen #1 and the left one is Screen #2. That means, that any window on the left screen has negative coordinates.
 
So I rewrote the function that way (should work for everyone, with or without dual screen):
- check the height and ensure it's not bigger than the desktop height. Resize if needed.
- check the width and ensure it's not bigger than the desktop width. Resize if needed.
- check the top and ensure it fits on the screen. Move to bottom if necessary.
- check the bottom (top + height) and ensure it fits on the screen. Move to top if necessary.
- check the left and ensure it fits on the screen. Move to right if necessary.
- check the right (left + width) and ensure it fits on the screen. Move to left if necessary.
 
Here's the code:
public void MoveIntoView()
{
	if (_windowHeight > System.Windows.SystemParameters.VirtualScreenHeight)
	{
		_windowHeight = System.Windows.SystemParameters.VirtualScreenHeight;
	}
 
	if (_windowWidth > System.Windows.SystemParameters.VirtualScreenWidth)
	{
		_windowWidth = System.Windows.SystemParameters.VirtualScreenWidth;
	}
 
	if (_windowTop < System.Windows.SystemParameters.VirtualScreenTop)
	{
		_windowTop = System.Windows.SystemParameters.VirtualScreenTop;
	}
	else if (_windowTop + _windowHeight >
		System.Windows.SystemParameters.VirtualScreenTop + System.Windows.SystemParameters.VirtualScreenHeight)
	{
		_windowTop = System.Windows.SystemParameters.VirtualScreenTop +
					 System.Windows.SystemParameters.VirtualScreenHeight - _windowHeight;
	}
 
	if (_windowLeft < System.Windows.SystemParameters.VirtualScreenLeft)
	{
		_windowLeft = System.Windows.SystemParameters.VirtualScreenLeft;
	}
	else if (_windowLeft + _windowWidth >
	   System.Windows.SystemParameters.VirtualScreenLeft + System.Windows.SystemParameters.VirtualScreenWidth)
	{
		_windowLeft = System.Windows.SystemParameters.VirtualScreenLeft +
					  System.Windows.SystemParameters.VirtualScreenWidth - _windowWidth;
	}
}
 
Hope it works for any config.
 
Fred
GeneralBehavior less than optimal on dual screen setupmemberDan Neely25 Jan '10 - 5:55 
If I put the dialog far enough off the screen that it's repositioned when loading the settings on my dual screen system instead of centering on one of the monitors it centers on the middle of the combined desktop area which results in it straddling the steam between the two displays.
 
3x12=36
2x12=24
1x12=12
0x12=18

GeneralRe: Behavior less than optimal on dual screen setupmemberJeremy Hutchinson25 Jan '10 - 6:06 
Yeah, that was by design. I move the window so half of the height and half of the width are visible. I figured that was enough that the user could grab it and do what they wanted with it.
GeneralRe: Behavior less than optimal on dual screen setupmemberstgn20 Jul '10 - 3:23 
In what cases is the MoveIntoView really necessary?
 
I tried two cases and both times the application windows was automatically moved to my main screen on a Windows 7 64bit system.
 
Case 1
------
Step 1: Laptop monitor on the left -- Desktop monitor on the right (set to main monitor).
Step 2: Move application window onto the left laptop monitor and close it so the settings are stored (_windowLeft will be negativ value).
Step 3: Under Display Settings, disable the left laptop monitor and set the desktop monitor to be the only monitor.
Step 4: Start the application.
Expected result: The window is off screen (to the left) and I can't access it.
Actual result: The window automatically appeared on the monitor.
 
Case 2
------
Same as Case 1 except that I positioned my Laptop monitor to be on the right (_windowLeft will be a positiv value).
Expected result: The window is off screen (to the right) and I can't access it.
Actual result: The window automatically appeared on the monitor.
 

Therefore: what did I miss? When is MoveIntoView necessary?
GeneralNIce idea, but I'm afraid its not a new onemvpSacha Barber8 Jan '10 - 23:28 
Josh Smith did this about 2 years ago:
 
http://joshsmithonwpf.wordpress.com/2007/12/27/a-configurable-window-for-wpf/[^]
 
Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

GeneralRe: NIce idea, but I'm afraid its not a new onememberBillWoodruff8 Jan '10 - 23:59 
Hi Sacha,
 
I read that article by Josh, after you cited it here. The article states : "source code was updated on 12/28/07)."
 
When you read the comments on the article (scroll down) it appears that several "difficulties" with the code design "as-is" were noted, and acknowledged by Josh, in 2008.
 
Unfortunately Josh's blog no longer has a valid way (comments are closed, no facility I can find to send him a PM on his site) for me to leave a query asking him if he did, in fact, update the code based on comments by Eric and others.
 
For someone who is a newcomer, like myself, to WPF, I would hesitate to study an article's code, now over two years old, where the author acknowledges and agrees with user comments reflecting some "problems" in the design of the code, but doesn't update the source code.
 
Which is not at all in any way to imply any criticism of Josh's writing : of course I'm sure he posted a working code example. And WPF today is a "long way" from the WPF of late 2007 (?).
 
best, Bill
 
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

GeneralRe: NIce idea, but I'm afraid its not a new onemvpSacha Barber9 Jan '10 - 0:08 
The code posted by Josh did work. I used it some time ago and it did the job.
 
Its no critique of this article as such, its just not a novel idea is all. This chap is just doing nothing new that Joshs didn't do.
 
As I say this is still cool.
 
Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

GeneralRe: NIce idea, but I'm afraid its not a new onememberJeremy Hutchinson9 Jan '10 - 3:01 
You're right Sacha, it is not a new idea. Chances are pretty good that I will never right an article where someone else has not written a similar article.
 
That being said I will continue to try to contribute to the community as best as I can.
GeneralRe: NIce idea, but I'm afraid its not a new onemvpSacha Barber9 Jan '10 - 5:04 
I am not having a go at all, sorry if it came of that way. This IS GOOD.
 
Sorry, my bad.
 
Apologies, I just wanted to show you Josh Smiths work in case there was something you missed I guess.
 
Sacha Barber
  • Microsoft Visual C# MVP 2008/2009
  • Codeproject MVP 2008/2009
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue
 
My Blog : sachabarber.net

GeneralIt's a better idea.memberMember 740954121 Oct '10 - 15:11 
Actually Josh Smith's implementation using inheritance is rather sloppy.
Generalthanks, and one questionmemberBillWoodruff8 Jan '10 - 19:14 
Hi Jeremy,
 
+5
 
Useful article ! Thanks for directing my attention to Settings feature of .NET which I have not really paid any attention to.
 
I think it would be very easy to use the same techniques in WinForms. : speaking as someone with almost no experience with WPF, I don't see anything really "WPF-centric" in what you are doing here.
 
Given there's one-and-only-one settings per app (?) I would be tempted to make the class that loads, saves, etc., static, and create an 'Initialization static method within that static class, but perhaps, in terms of WPF, that's a "bad thing" ?
 
Again, thanks !
 
best, Bill
 
"Many : not conversant with mathematical studies, imagine that because it [the Analytical Engine] is to give results in numerical notation, its processes must consequently be arithmetical, numerical, rather than algebraical and analytical. This is an error. The engine can arrange and combine numerical quantities as if they were letters or any other general symbols; and it fact it might bring out its results in algebraical notation, were provisions made accordingly." Ada, Countess Lovelace, 1844

GeneralRe: thanks, and one questionmemberJeremy Hutchinson9 Jan '10 - 3:02 
I don't see any reason why this could not have been done using a static class.
GeneralThanks for the time and effort of writing this articlemembercyberdyme7 Jan '10 - 23:44 
Thanks for the time and effort of writing this article
GeneralMy vote of 1memberhaadikhan27 Jan '10 - 8:52 
none
GeneralRe: My vote of 1mvpPete O'Hanlon7 Jan '10 - 9:21 
haadikhan2 wrote:
none

 
Well, that's not a helpful comment now is it? If you don't like the article then you should at least have the courtesy to explain to the author why you don't like it. He's taken the time to write this, and has put his ego on the line here - the least you can do is give him your reasons for pissing on his parade - now grow yourself a backbone and clarify your comment - or remove your vote.
 

"WPF has many lovers. It's a veritable porn star!" - Josh Smith

As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

My blog | My articles | MoXAML PowerToys | Onyx



GeneralRe: My vote of 1memberJeremy Hutchinson7 Jan '10 - 9:43 
Thanks for that.
 
I took a look at person's profile, and it appears they have been a member for most of the day and have 1 voted two articles for "no reason".
GeneralRe: My vote of 1memberTonyJ7 Jan '10 - 10:48 
There should be a way to ban users if they are challenged to provide a valid reason for a low vote and they
fail to do so.

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

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