WindowRestorer ClassCleanCode C# Libraries v1.2.03 API
Tracks a form's window state and position and enables an application to restore it upon subsequent invocations; provides enhanced support for maximizing across multiple monitors; enables locating subforms on the same monitor as a main form.
Inheritance Hierarchy

OnlineSystem Object
  CleanCode.Forms WindowRestorer

Namespace: CleanCode.Forms
Assembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
Syntax

public class WindowRestorer
Remarks

These remarks are subdivided into the two major areas: window state persistence and multiple monitor support.

Window State Persistence

Used in conjunction with application settings (see OnlineApplication Settings for Windows Forms) this helper class accounts for multiple monitors, changes in monitors, and whether the window is minimized or maximized at the time the application is terminated. The code skeleton below illustrates the key points involved in wiring up this class.

  • Instantiate a WindowRestorer supplying values for the window state and position at the time the application was last terminated, which come from persistent application settings. The supplied values are used as a starting point. If the application was minimized, it is restored in a normal state. If it was normal or maximized, it is restored to that same state. If the monitor configuration has changed so that the window is no longer visible, it is moved so that it is visible.
  • Wire up the window Move and Resize events to track changes while you use the application.
  • Wire up the FormClosing event to save the window state and position at the time the application is terminated, for use during the following invocation.
private WindowRestorer windowRestorer;

public MainForm()
{
    . . .
    windowRestorer = new WindowRestorer(this,
        Properties.Settings.Default.WindowPosition,
        Properties.Settings.Default.WindowState);
}

protected override void OnMove(EventArgs e)
{
    base.OnMove(e);
    if (windowRestorer != null) windowRestorer.TrackWindow();
}

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    if (windowRestorer != null) windowRestorer.TrackWindow();
}

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
    SaveUserSettings();
    . . .
}

private void SaveUserSettings()
{
    Properties.Settings.Default.WindowPosition = windowRestorer.WindowPosition;
    Properties.Settings.Default.WindowState = windowRestorer.WindowState;
    . . .
    Properties.Settings.Default.Save();
}

Multiple Monitor Support

Maximizing Across Multiple Monitors

You can enhance a form's standard maximize button so that when you depress Control with the maximize button, the form maximizes across multiple monitors (horizontally-arranged) instead of just the current monitor. Enabling this functionality requires adding just one line to the above code. Change the OnResize method to this, adding the one highlighted line:

protected override void OnResize(EventArgs e)
{
    base.OnResize(e);
    if (windowRestorer != null)
    {
        windowRestorer.MultipleMonitorMaximize();
        windowRestorer.TrackWindow();
    }
}

The multiple-monitor maximization works intelligently across multiple monitors of varying resolution with a logical side-by-side organization (vertically-stacked monitors are not supported). When you invoke this feature, the resolutions of each screen are examined; the smallest one drives the height of the maximized window. The window expands to fill the smallest one vertically but no further. (If it expanded to fill the largest monitor, you would then have to frequently scroll back and forth vertically to see the whole window.) The code takes into account whether you have aligned your different resolution monitors (using the standard Windows Display control panel) by their tops or their bottoms.

A conventional window remembers only two sizes: the normal size and the maximized size. With this enhanced functionality, however, your windows remember three sizes: normal, maximized, and multi-maximized. So from the normal state, repeated presses of Maximize will toggle between normal and maximized. Similarly, repeated presses of Control-Maximize will toggle between normal and multi-maximized. Finally, you can toggle between multi-maximized and maximized by doing Ctrl-Max, then Max, then Ctrl-Max, then Max, etc.

Keeping subforms on the same monitor as the main form

WindowRestorer also provides a mechanism for automatically positioning your custom subforms of your main application window onto the same monitor (unfortunately, it does not do this for the native .NET open and save file dialogs). Just invoke the static SetSubWindowRelativeLocation(Form, Point, Int32, Int32) method right before you display your subform.

Since CleanCode 0.9.28.

See Also