In most of the articles in the WPF tutorial we have used objects of the Window class as containers for the controls that we have been examining. Windows themselves are controls, inheriting from ContentControl. However, they are not designed to be embedded within a container; they are used as standalone items.
A Window object describes a window with all of the elements that you would expect to see in any application. You can include a title bar that includes an icon in the top-left corner, title text, and maximise, minimise and close buttons at the top-right. The main area of the window is the content area, where you add controls. The window can also include various styles of border, including one with a sizing grip that makes it easier to resize the window using the mouse.
The Window control also affects items outside of the rectangle of the window itself. For example, the icon and title from the window are also used in the operating system's task bar and the title affects the area that appears when you switch between applications using Alt-Tab. You can also configure windows so that they do not appear in the task bar at all. This is ideal for custom dialog boxes or simple utilities that minimise to the system tray.
In this article we will look at the basic features of a Window. Over the following few articles we'll expand upon this, looking at more features of the class. To begin, create a new WPF application in Visual Studio named, "WindowDemo". Once the project is ready, replace the XAML in the main window with the following code:
Title="Window Demo" Height="150" Width="200">
<TextBlock Name="Message" FontSize="25">Hello, World!</TextBlock>
The XAML produces a window containing a TextBlock. The window includes values in three key properties, which we have used extensively throughout the tutorial. Height and Width set the size of the window. These are inherited from FrameworkElement. Title is a member of the Window class. It holds a string containing the text to display in the title bar of the window. You can see the title, "Window Demo" when you run the program.
Changing the Window's Icon
In the previous image you can see an icon at the top-left corner of the window. If you do not choose one, the icon associated with the application or assembly is displayed. As we have not added an assembly icon, we see the default image seen above.
Icons files are generally more complicated than simple pictures. They can include multiple images, each with different dimensions and varying numbers of colours, called bit depths. Windows automatically selects the most appropriate image before rendering it.
For example, for a standard configuration on a modern computer, the icon shown in the corner of a window will be a 16x16 pixel item with millions of colours. For the taskbar, a 32x32 or 48x48 pixel icon might be selected. If the operating system is configured with a low number of available colours, an icon with 256 or even 16 colours might be chosen. Having multiple images in the icon file allows you to achieve the best results in all scenarios. However, if you do not provide every possible size and bit depth in the file, an alternative image will be used. This might lead to pixellation or poor colour reproduction in some situations.
An application looks better when it uses custom icons. To add one for a window, you need to add a suitable file to the project and reference it with the window's Icon property.
Let's add an icon to the window. Find an icon file with the .ico extension and add it to the root of your project by right-clicking the project in the solution explorer and electing to add an existing item. Once you have added the file, modify the Window element's opening tag, as shown below, to set the Icon property. In the example the icon is named, "BlackWasp.ico".
NB: If you cannot find a suitable icon, the BlackWasp.ico file is included in the sample code, which you can download using the link at the start of this article.
Title="Window Demo" Height="150" Width="200"
Run the program to see the results. The icon should appear in the corner of the window and in the task bar.
27 June 2015