Search
Friday, March 02, 2001 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Disclosure  
     
  
   TechTidBits (Blog)  

Saving Window State/Location/Size and Restoring On Reload

Feb 20

Written by:
Friday, February 20, 2009 11:00 PM  RssIcon

Today I read an article called Saving and Restoring Form Location in a Multi-Monitor Environment. The article talks about saving and restoring the previous window's size and location even on systems with multiple monitors.  I have a different solution to this problem from the author, please read click through for more imformation and the code sample.

Today I read an article called Saving and Restoring Form Location in a Multi-Monitor Environment. The article talks about saving and restoring the previous window's size and location even on systems with multiple monitors. Have you noticed some apps "behave well" when you restart them? What I mean by that is, they remember where they were previously and reopen themselves predictably.

Try this small test out and see if you get the same results I do. Fire up the following applications, resize and move them on your first monitor, close them down, then try reopening them again. Do you see the same behaviour as I do? Now try it again but over on your second monitor, I think you'll be surprised at just how many tools you use that don't behave properly.

Application 1st Monitor 2nd Monitor
Size Location Window State Size Location Window State
Notepad (notepad.exe) Y Y N Y Y N
PaintBrush (pbrush.exe) Y Y Y Y N* N*
Calculator (calc.exe) NA X NA NA N NA
Windows Explorer (explorer.exe) Y Y Y Y Y Y
Programs and Features (Vista) Y Y Y Y Y Y
* = When PaintBrush (PBrush.exe) was reopened on the 2nd monitor, the location and state were not restored onto the same monitor but onto the first monitor.

Notice how Windows Explorer and Programs and Features (Add Programs and Features in XP) are the only two on that list which "behave properly." Meaning, when you reopen them, they behave predictably, the open up in exactly the same spot, in the exactly the same way they were closed.

Although nicely written by NealSchafer and it is nice code, I believe there is a simpler solution to this problem. Neal's solution to this problem is conscise, however I believe there is a better way to solve this issue and making it multi-user friendly on a box that has multiple users.

The solution I'm demoing is using appliation properties scoped by user, and I'm taking advantage of the ints returned by .NET and stored by the system to take into account multiple monitors (the Location.Left is returned with the correct value even on multiple monitors).

There are three key pieces to this solution

  1. Setup the Project Settings and with appropriate defaults, notice in the diagram below how the "values" are setup to pass to the constructor for the type you're setting. This is also where you would set the scope to Application instead of User if appropriate to have a global setting (Connection String might be an example). Notice how the popular data type are listed front and center, that's cool! But if you don't see your data type you can use the Browse option at the bottom.
  2. Query those values by using the Namespace.Properties.Settings.Default.ProjectSettingName (SavingWindowLocSizeState.Properties.Settings.Default.Location in this example) checking for values/nulls/what ever is appropriate for that data type and then set the property you want to set. It looks weird but the ".Default" part has to be there, trust me, try it without and it'll break.
  3. Save the settings on the FormClosing event to make sure you get those settings back into the user settings so they can be reused when the application reopens.

 Project settings.

I've thrown in a few other pieces of code, cause, well, I felt like it and they made sense to me. :>
  • Button to clear out the values so you can start over from scratch. You'll be surprised that you cannot set these values to null, the project won't compile, so set logical values or pick logical sources (like MinimumSize)
  • Flag in code to check if those values indeed need to be saved out to the default properties, this is only in the code to support the previous bullet point, you can freely remove this if you don't care about resetting the values
  • An image of my current company (Privasoft) and some code to launch the webpage as well as change the cursor, hey I'm having fun with this! :> (To be honest I'm hoping you have fun with it too! :>)

I hope this helps you with your next Win Apps.

Grab a coffee and get coding! :>

 Resources:

Sample Code on my server (SVN Server)

If you're using SVN client, then use the following command to get the source code into the current directory.

svn checkout http://www.pchenry.com:8080/svn/Blog/trunk/2009/SavingWindowLocSizeState/

The Code Project: Saving and Restoring Form Location in a Multi-Monitor Environment

 

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

2 comment(s) so far...


Gravatar

Re: Saving Window State/Location/Size and Restoring On Reload

Hi

Is the code still available? I can not access it :-/

By Lord Quo on   Monday, February 11, 2013 11:44 AM
Gravatar

Re: Saving Window State/Location/Size and Restoring On Reload

Yes, I still have the code, but unfortunately I haven't checked it into tfs on codeplex yet. :< If you need/want it, please leave me another comment with your email (but put in english using "at" for the @ sign, and "dot" for the period to try to confuse the spammers haha). Then I can email you a zip of it. Thanks for stopping by and commenting!

By Peter Henry on   Thursday, February 28, 2013 9:53 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement