Correctly written Windows apps that want to save their location from run to run will save the results of GetWindowPlacement()
before shutting down, then use SetWindowPlacement()
on startup to restore their position.
Frequently, apps will store the results of GetWindowPlacement()
in the registry as a REG_BINARY
for easy use.
The WINDOWPLACEMENT
route has many advantages over other methods:
- Handles the case where the screen resolution changed since the last run:
SetWindowPlacement()
will automatically ensure that the window is not entirely offscreen
- Saves the state (minimized/maximized) but also saves the restored (normal) size and position
- Handles desktop metrics correctly, compensating for the taskbar position, etc. (i.e. uses "workspace coordinates" instead of "screen coordinates" -- techniques that rely on saving screen coordinates may suffer from the "walking windows" problem where a window will always appear a little lower each time if the user has a toolbar at the top of the screen).
Finally, programs that handle window restoration properly will take into account the nCmdShow
parameter passed in from the shell. This parameter is set in the shortcut that launches the application (Normal, Minimized, Maximize):
if(nCmdShow != SW_SHOWNORMAL)
placement.showCmd = nCmdShow; //allow shortcut to override
For non-Win32 applications, it's important to be sure that the method you're using to save/restore window position eventually uses the same underlying call, otherwise (like Java Swing's setBounds()
/getBounds()
problem) you'll end up writing a lot of extra code to re-implement functionality that's already there in the WINDOWPLACEMENT
functions.