Window Visibility and State

You can control the visibility of a window with the Visibility property, which has the following values from the Visibility enumeration: Visible, Hidden, and Collapsed. (The Window class treats Hidden and Collapsed as the same.) You can also use the Show and Hide methods. In addition to stopping the window from rendering, the Hide method takes it out of the taskbar (assuming the ShowInTaskbar property is set in the first place).

If you'd like to hide the window but leave it in the taskbar, you can set the WindowState property to the WindowState enumeration value Minimized. To restore it or make it take up the whole desktop (minus the taskbars and the sidebar), you can use Normal or Maximized.

Likewise, as the user interacts with the window, the WindowState will reflect the current state of the window. If, while the window is minimized or maximized, you'd like to know the location and size of what it will be upon restoration, you can use the RestoreBounds property. Unlike Left, Top, Width, Height, ActualWidth, and ActualHeight, all of which will reflect the current window state within the min/max bounds, RestoreBounds changes only if the window is moved or resized while it's in the Normal state. This makes RestoreBounds a handy property to keep in a user setting for window restoration,* as you can see in Example 10-3.

Example 10-3. Saving and restoring window state public partial class MainWindow : System.Windows.Window { public MainWindow() { InitializeComponent( );

// Restore state from user settings

Rect restoreBounds = Properties.Settings.Default.MainRestoreBounds;

WindowState = WindowState.Normal;

Left = restoreBounds.Left;

Top = restoreBounds.Top;

Width = restoreBounds.Width;

Height = restoreBounds.Height;

WindowState = Properties.Settings.Default.MainWindowState;

// Watch for main window to close Closing += window_Closing;

* Chapter 2 describes user settings.

Example 10-3. Saving and restoring window state (continued) // Save state as window closes void window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { Properties.Settings.Default.MainRestoreBounds = RestoreBounds; Properties.Settings.Default.MainWindowState = WindowState; Properties.Settings.Default.Save();

This code assumes a couple of user settings variables named MainRestoreBounds and MainWindowState of type Rect and WindowState, respectively. With these in place, when the main window starts up, we set the window state temporarily to Normal so that we can set the left, top, width, and height from the restored bounds. After we do that, we can set the window state to whatever it was when we last ran the application, all of which happens before the window is shown, so there's no shake 'n' shimmy as the window goes between normal and another state. When the window is closing (but before it's closed), we tuck the data away for the next session and save the settings. Now, no matter what state the main window is in when we close it, we properly remember that state and the restored state.*

So far, we've talked about windows in the abstract, referring to some usages (e.g., top-level windows, toolbox windows, main windows, etc.). One other large use for windows in Windows applications is the humble dialog, which is the subject of the rest of this chapter.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment