Window State Components
|Date:||28 October 2014|
The window state components can save and restore a window's size, position and state (minimized, maximized or normal) between program executions. Three components are provided that use different means of storing the information. They are:
- Records window information in an ini file. The user has control over the ini file name (via the IniFileName property) and the name of the section of the ini file where window information is recorded (using the Section property). Alternatively the ini file and section names can be configured by handling the OnGetIniData event that is triggered immediately before the ini file is read or written.
Uses the registry to record window information. The registry root key
and sub key where the information is stored are controlled by the
RootKey(Ex) and SubKey properties, or by handling the
OnGetRegData(Ex) event. This event is triggered just before the
registry is accessed. Additional application defined data can be read
from or written to the registry by handling the
OnGettingRegData and OnPuttingRegData events that
are triggered after the component reads or writes the registry.
Important Note: You are discouraged from using TPJRegWdwState for programs compiled with Delphi 5 and earlier that may be run on 64 bit Windows, because the component may not be able to access the 64 bit registry view correctly and window state may be lost.
- When using this component the user must handle saving and reading the window state data to or from persistent storage. The component gives the most flexibility of all the components at the expense of placing the storage burden on the user. The component triggers OnReadData and OnSaveData events when it is ready to read or save data.
All components implement the same functionality, controlled by some common properties and events, as follows:
- The components can automatically restore and save windows when the program starts up and closes down (using the AutoSaveRestore property). If AutoSaveRestore is set to False then the Restore and Save methods must be called from the host application.
The Options property can be used to customise the way the
window is restored:
- The components can be instructed to ignore the saved window state – the window is then displayed in the normal state.
- The window's saved size can be ignored and the default size of the form used instead. This is useful for dialog boxes and fixed size windows.
- The window can be kept within the current work area of the desktop. If this option is used the window also appears on the correct monitor on multi-monitor systems. If the form containing the window state component is a MDI child form this option keeps the window within the parent form's client area, after allowing for any menu, toolbars or status bar etc.
- When the form is to be restored in a minimized state it briefly appears on screen in the normal state before being minimized. The MinimizeDelay property controls the delay between the window appearing and being minimized.
TPJWdwState and TPJRegWdwState also support the OnReadWdwState event. Handling this event enables the stored window's state, size and position values to be changed before the window is restored. This event is called after reading the data and before sizing the window. TPJUserWdwState does not expose this event because the user is in charge of reading the data and can modify it in the OnReadData event.
All the components derive from an abstract base class named TPJCustomWdwState. This class provides the core window handling and sizing functionality. It provides abstract methods for accessing the required storage medium. Therefore it is quite straightforward to create further components that use alternative storage systems. All that needs to be provided are methods to read/write the window information along with any additional properties that are required to configure the storage medium.
Note that these components are 64 bit compatible and can be included in VCL packages targetted at 64 bit Windows using Delphi XE2 or later.
The components are fully documented online.
There is also an FAQ page hosted on the DelphiDabbler Wiki.
How the components work
The underlying principle used in the code is described in the article How to remember a window's size, state and position.
We can't rely on Delphi's own Width and Height properties for recording window size, since these do not maintain size of the normalised window size when the window is maximized. Instead we have to use the GetWindowPlacement and SetWindowPlacement Windows API calls.
Finding the right time to restore a window on application startup has proved problematic. It is not possible to call the Restore method before the form's window handle has been created. We therefore need to delay any such restoration until the form has been shown. A hook class is used to peek at the Windows messages sent to the parent form. This class forwards relevant messages to the component.
Note: One side effect of this complexity is that you can't create instances of the components at run time using the standard constructor. The code is not initialised properly unless the component is added to a form at design time. A special constructor – CreateStandAlone – has been provided that makes dynamic construction possible.
There are four demo projects included with these components. They are:
Demonstrates how to use the components in the standard way, i.e. dropped on a form from the component palette. This demo uses TPJRegWdwState.
Demonstrates how to create and use the components dynamically using the CreateStandAlone constructor. This demo uses TPJWdwState.
Demonstrates how to use TPJUserWdwState and load and save data in the OnReadData and OnSaveData events.
Demonstrates the use of TPJWdwState with MDI applications.
Feedback & Queries
If you find any bugs or want to suggest a new feature please report them using the Issue Tracker.
Click the Create Ticket button to create a ticket then complete the
form giving as much information as possible. Please note:
this issue tracker is shared among all the projects in the DelphiDabbler
Code Library, so please make sure you enter
wdwstate in the
Project edit box. You should also choose
Defect in the
Type drop down list to report a bug or
request a new feature.
If you have created a bug fix or have implemented a new feature please attach a zip file to your ticket that contains your source code.
If you can't find an answer in the documentation then post a message in the discussion group.