Basically when user resizes my application’s window I want application to be same size when application is re-opened again.
At first I though of handling SizeChanged event and save Height and Width, but I think there must be easier solution.
Pretty simple problem, but I can not find easy solution to it.
Save the values in the user.config file.
You’ll need to create the value in the settings file – it should be in the Properties folder. Create five values:
Topof typedoubleLeftof typedoubleHeightof typedoubleWidthof typedoubleMaximizedof typebool– to hold whether the window is maximized or not. If you want to store more information then a different type or structure will be needed.Initialise the first two to 0 and the second two to the default size of your application, and the last one to false.
Create a Window_OnSourceInitialized event handler and add the following:
NOTE: The set window placement needs to go in the on source initialised event of the window not the constructor, otherwise if you have the window maximised on a second monitor, it will always restart maximised on the primary monitor and you won’t be able to access it.
Create a Window_Closing event handler and add the following:
This will fail if the user makes the display area smaller – either by disconnecting a screen or changing the screen resolution – while the application is closed so you should add a check that the desired location and size is still valid before applying the values.