Window Location and Size

You can manage the x and y locations of a window with the Top and Left properties, whereas you can influence the Z order with the TopMost property. Across the entire desktop, all windows with the TopMost property set to true appear above all of the windows with the TopMost property set to false, although the Z order of the windows within their layer is determined by user interaction. For example, clicking on a non-topmost window will bring it to the top of the non-topmost layer of windows, but will not bring it in front of any topmost windows.

If you'd like to set the startup location of a window manually, you can do so by setting the Top and Left properties before showing the window. However, if you'd prefer to simply have the window centered on the screen or on the owner, you can set the WindowStartupLocation property to CenterScreen or CenterOwner as appropriate before showing the window:

Window1 window = new Window1();

window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show(); // will be centered on the screen

If you don't change the WindowStartupLocation, Manual is the default. Manual lets you determine the initial position by setting Top and Left. If you don't care about the initial position, the Manual value lets the Windows shell determine where your window goes.

You can get the size of the window from the ActualWidth and ActualHeight properties of the Window class after (not during) the Initialized event (e.g., Loaded is a good place to get them). The actual size properties are expressed (like all Window-related sizes) in device-independent pixels measuring 1/96th of an inch.* However, the ActualWidth and ActualHeight properties are read-only, and therefore, you cannot use them to set the width and height. For that, you need the Width and Height properties.

The size properties (Width and Height) are separate from the "actual" size properties (ActualWidth and ActualHeight) because the actual size is calculated based on the size, the minimum size (MinWidth and MinHeight), and the maximum size (MaxWidth and MaxHeight). For example:

Windowl window = new Windowl();

window.Show(); // render window so ActualWidth is calculated window.MinWidth = 200; window.MaxWidth = 400; window.Width = 100;

Debug.Assert(window.Width == 100); // regardless of min/max settings Debug.Assert(window.ActualWidth == 200); // bound by min/max settings

Here, we've set the width of the window outside the min/max bounds. The value of the Width property doesn't change based on the min/max bounds, but the value of the ActualWidth property does. This behavior lets you set a size outside of the min/ max bounds, and that size will stick and potentially take effect if the min/max bounds change. This behavior also means that you should set the Width and Height properties to influence the size of your window, but you'll probably want to get the ActualWidth and ActualHeight properties to see what kind of influence you've had.

If, instead of sizing the window manually, you'd like the size of the window to be initially governed by the size of the content, you can change the SizeToContent property from the default SizeToContent enumeration value Manual to one of the other three enumeration values: Width, Height, or WidthAndHeight. If the content size falls outside the min/max bounds for one or both dimensions, the min/max bounds will still be honored. Likewise, if Width or Height is set manually, the SizeToContent setting will be ignored.

By default, all windows are resizable; however, you can change the behavior by setting the ResizeMode property with one of the values from the ResizeMode enumeration: NoResize, CanMinimize, CanResize, or CanResizeWithGrip, as shown in Figure 10-3.

You'll notice from Figure 10-3 that the NoResize resize mode causes the Minimize and Maximize buttons to go away. It also doesn't resize at the edges. Likewise, CanMinimize doesn't allow resizing at the edges, but it shows the Minimize and Maximize buttons (although only the Minimize button is functional). On the other hand, the CanResize mode makes the window fully resizable, whereas CanResizeWithGrip is just like CanResize except that it puts the grip in the lower-righthand corner. Because of the huge advances that WPF has provided in the area of layout functionality, there should be almost no reason to use a resize-disabling resize mode.

* For example, if you set the size of a WPF window to 400 x 400 in 120dpi mode, according to Spy++, the size of that window will be 500 x 500 as far as the operating system is concerned. However, at 96dpi, 400 x 400 is 400 x 400 for both WPF and the OS.

Figure 10-3. Window resize modes

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