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:

NameTypeDefault value

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:

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

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

private void _RestoreWindow()
            if (Properties.Settings.Default.WindowHeight == 0
                || Properties.Settings.Default.WindowWidth == 0)
            this.Height = Properties.Settings.Default.WindowHeight;
            this.Width = Properties.Settings.Default.WindowWidth;
            this.Left = Properties.Settings.Default.WindowLocationX;
            this.Top = Properties.Settings.Default.WindowLocationY;
            if (Properties.Settings.Default.WindowState == "Normal")
                this.WindowState = WindowState.Normal;
            else if (Properties.Settings.Default.WindowState == "Maximized")
                this.WindowState = WindowState.Maximized;

        private void mainWindow_Closing(object sender, CancelEventArgs e)
            Properties.Settings.Default.WindowWidth = this.Width;
            Properties.Settings.Default.WindowHeight = this.Height;
            Properties.Settings.Default.WindowLocationX = this.Left;
            Properties.Settings.Default.WindowLocationY = this.Top;
            Properties.Settings.Default.WindowState = Convert.ToString(this.WindowState,
            Properties.Settings.Default.Save(); // ensure that your settings are written to the disk

That's it - job done!

Tags: computers
Categories: None |

0 comments have been posted.

Your email: we will send you a confirmation link to this address to confirm your identity and to prevent robot posting
Get in touch
Join notification list

Join the email list to get notified about new posts and other updates

Follow updates

Join our social networks and RSS feed to keep up to date with latest news and publications