Click here to Skip to main content
15,884,962 members
Articles / Desktop Programming / WPF

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

Rate me:
Please Sign up or sign in to vote.
4.88/5 (48 votes)
3 Aug 2012CPOL2 min read 194.5K   4.3K   68  
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.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="SaveWindowPosition.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <userSettings>
        <SaveWindowPosition.Properties.Settings>
            <setting name="WindowTop" serializeAs="String">
                <value>100</value>
            </setting>
            <setting name="WindowLeft" serializeAs="String">
                <value>100</value>
            </setting>
            <setting name="WindowHeight" serializeAs="String">
                <value>300</value>
            </setting>
            <setting name="WindowWidth" serializeAs="String">
                <value>300</value>
            </setting>
            <setting name="WindowState" serializeAs="String">
                <value>Normal</value>
            </setting>
        </SaveWindowPosition.Properties.Settings>
    </userSettings>
</configuration>

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)


Written By
Software Developer
United States United States
I’m a Software Engineer at Microsoft working on the Azure Portal. Before that I spent about 20 years developed various business applications at a number of different companies. I have a passion for writing clean, scalable code and sharing what I’ve learned with others.

I also help run the Casco Bay .Net User Group

Comments and Discussions