Window State Components

   
Release: 5.3.1
Date: 15 November 2009
Delphi Compatibility:
 D1   D2   D3   D4   D5   D6   D7  D2005
(Win32)
D2006
(Win32)
D2007 D2009
(Win32)
D2010
(Win32)
Red LED Red LED Red LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED Green LED

Overview

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:

TPJWdwState
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.
TPJRegWdwState
Uses the registry to record window information. The registry root key and sub key where the information is stored are controlled by the RootKey and SubKey properties, or by handling the OnGetRegData 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.
TPJUserWdwState
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.

Documentation

The components are documented in the Window State Components Wiki.

Users of Delphi version 4 to 7 can also make use of the help file that is included in the download. It integrates with the Delphi 4-7 help system. The help file is a WinHelp file and therefore does not work on Windows Vista unless WinHelp has been downloaded and installed. See Microsoft Knowledge Base Article KB917607 for further information.

Property editor

A property editor is available that makes it easy to edit the TPJRegWdwState.RootKey property which has type HKEY. The property editor is available here.

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.

Demo programs

There are four demo projects included with these components. They are:

  1. StandardDemo.dpr
    Demonstrates how to use the components in the standard way, i.e. dropped on a form from the component palette. This demo uses TPJRegWdwState.
  2. StandAloneDemo.dpr
    Demonstrates how to create and use the components dynamically using the CreateStandAlone constructor. This demo uses TPJWdwState.
  3. UserDemo.dpr
    Demonstrates how to use TPJUserWdwState and load and save data in the OnReadData and OnSaveData events.
  4. MDIDemo.dpr
    Demonstrates the use of TPJWdwState with MDI applications.

Feedback

If you find any bugs or want to suggest a new feature please report them using the Issue Tracker. If you have created a bug fix or have implemented a new feature please attach a zip file containing your source code.

If your have any other comments please contact me.