How to get window original size and position (wsNormal vs wsMaximized)

delphi, winapi, windows

Solution

The functions that you need are `GetWindowPlacement` and `SetWindowPlacement`. These operate on the `WINDOWPLACEMENT` struct. That struct has the `rcNormalPosition` member which is documented like this:

The window's coordinates when the window is in the restored position.

When you save away the window position and size you need to save away the values found in `rcNormalPosition`. In the opposite direction, when restoring the window position and size you must call `SetWindowPlacement` passing as `rcNormalPosition` the value stored in the user preference in the INI file.

Problem

I'm looking to store user personalization of window size and position to use it when the application is reopened. That's actually very simple and I have the following code working: OnCreate: ``` Width := wIni.ReadInteger('FORM', 'FORMW', 980); Height := wIni.ReadInteger('FORM', 'FORMH', 500); PnlXMLI.Width := wIni.ReadInteger('FORM', 'PNLXMLIW', 500); WindowState := TWindowState(GetEnumValue(TypeInfo(TWindowState), wIni.ReadString('FORM', 'WINDOWSTATE', 'wsNormal'))); ``` OnDestroy: ``` wIni.WriteInteger('FORM', 'FORMW', Width); wIni.WriteInteger('FORM', 'FORMH', Height); wIni.WriteInteger('FORM', 'PNLXMLIW', PnlXMLI.Width); wIni.WriteString('FORM', 'WINDOWSTATE', GetEnumName(TypeInfo(TWindowState), Ord(WindowState))); ``` The problem is that when the user maximizes the window, and then restore it, it goes back to the size it was before maximizing. But if he maximizes, and then close and reopen the application and restore it, the application will not go back to original size before maximizing. It will be at the size of screen, because Width and Height properties gives the maximized size when read. Question is: how can I get the original size of the window, i.e., the one it will go back to when the user restore the window? I tried setting WindowState to wsNormal right before reading the Width and Height, but it didn't work (maybe because the form is being destroyed?)... and I'd rather use a solution that doesn't do unnecessary GUI operations (for source code aesthetic reasons). Thanks in advance.

Original source