left-icon

WPF Succinctly®
by Buddy James

Previous
Chapter

of
A
A
A

CHAPTER 1

WPF Origins

WPF Origins


A brief history before WPF

The only way to fully understand where you are is to understand where you’ve been. WPF has completely revolutionized the way that we develop desktop applications. I’m not simply talking about writing applications, but core changes in the way that the operating system renders the GUI of your applications.

User32 and GDI/GDI+

GDI/GDI+ has provided drawing support to the Windows OS for things such as images, shapes, and text. These technologies are known to be relatively complicated to use and they provide poor performance. The User32 subsystem has provided the common look and feel for Windows elements such as buttons, text boxes, windows, etc.

Any framework that came along before WPF simply provided optimized wrappers to GDI/GDI+ and User32. These wrappers provided improved APIs, but the performance was still poor as the underlying technologies were the same.

And then there was DirectX

Microsoft created DirectX in an effort to provide a toolkit to allow developers to create video games that could bypass the limitations of the GDI/User32 subsystems. The DirectX API was extremely complex and therefore it was not ideal for creating line-of-business application user interfaces.

WPF to the rescue

WPF is the most comprehensive graphical technology change to the Windows operating system since Windows 95. WPF uses DirectX behind the scenes to render graphical content, allowing WPF to take advantage of hardware acceleration. This means that your applications will use your GPU (your graphics card’s processor) as much as possible when rendering your WPF applications.

User32 in a limited capacity

WPF uses User32 in a limited capacity to handle the routing of your input controls (your mouse and keyboard, for instance). However, all drawing functions have been passed on through DirectX to provide monumental improvements in performance.

WPF and Windows 7/Windows Vista

WPF will perform best under Windows 7 or Windows Vista. This is because these operating systems allow the technology to take advantage of the Windows Display Driver Model (WDDM). WDDM allows scheduling of multiple GPU operations at the same time. It also provides a mechanism to page video card memory with normal system memory when the video card memory threshold is exceeded.

One of the first jobs of the WPF infrastructure is to evaluate your video card and provide a score or rating called a tier value. WPF recognizes three distinct tier values. The tier value descriptions follow, as provided by the Microsoft Developer Network documentation on WPF, available at https://msdn.microsoft.com/en-us/library/ms742196(v=vs.100).aspx:

Rendering Tier 0: No graphics hardware acceleration. All graphics features use software acceleration. The DirectX version level is lower than version 9.0.

Rendering Tier 1: Some graphics features use graphics hardware acceleration. The DirectX version level is higher than or equal to version 9.0.

Rendering Tier 2: Most graphics features use graphics hardware acceleration. The DirectX version level is higher than or equal to version 9.0.

The System.Windows.Media.RenderCapability.Tier property

To programmatically retrieve the rendering tier, you will need to use the static System.Windows.Media.RenderCapability.Tier property. You have to shift the property by 16 bits to access the tier value. These extra bits allow for extensibility, such as the possibility of storing information regarding support for other features or tiers in the future. To illustrate, we will create a WPF application. I will illustrate how to create a WPF project step by step, as this will be the starting point for each example throughout the entire book. Please note: I’m using Visual Studio 2010 Professional; however, the steps should be the same regardless of the version.

Open Visual Studio and select New Project.

New Project Window

  1. New Project Window

A WPF application contains a window as well as a code behind. The MainWindow.xaml file contains the XAML markup. The MainWindow.xaml.cs file contains the code-behind logic. The idea of a code behind should be familiar to you if you have any experience creating Windows Forms applications. The XAML markup may seem confusing at first, but don’t worry, we will cover the markup in the chapters to come.

Here is a screenshot of the MainWindow.xaml file in design mode:

MainWindow.xaml File in Design Mode

  1. MainWindow.xaml File in Design Mode

Tip: In Design mode, you can split the designer to see the rendered output at the top and the XAML markup at the bottom.

To the right in the Solution Explorer you will find the MainWindow.xaml and MainWindow.xaml.cs code-behind files. First, we’ll start with the XAML.

<Window x:Class="Chapter01.MainWindow"

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       Title="MainWindow" Height="134" Width="515">

    <Grid>

        <Grid.RowDefinitions>

            <RowDefinition Height="36*" />

            <RowDefinition Height="91*" />

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition />

            <ColumnDefinition />

        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Rendering Tier" />

        <TextBox Grid.Row="0" Grid.Column="1" x:Name="txtRenderingTier" />

        <Button x:Name="btnGetRenderingTier" Content="Get Rendering Tier" Grid.Row="1" Grid.Column="1" Width="130" Height="30" Click="btnGetRenderingTier_Click" />

    </Grid>

</Window>

Note: The WPF Window can be compared to the WinForms Form object.

This is the XAML (Extensible Application Markup Language) that defines the user interface. XAML is an XML-based markup language, and is covered in detail in Chapter 2. Basically we have a label control, a textbox control, and a button. The grid control is used to control the layout of the other controls. The main unit of display that we are using in this example is a WPF Window.

Here are the contents of the MainWindow.xaml.cs code behind:

using System.Windows;

using System.Windows.Media;

namespace Chapter01

{

    /// <summary>

    /// Interaction logic for MainWindow.xaml.

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }

        private void btnGetRenderingTier_Click(object sender, RoutedEventArgs e)

        {

            //Shift the value 16 bits to retrieve the rendering tier.

            int currentRenderingTier = (RenderCapability.Tier >> 16);

            switch (currentRenderingTier)

            {

                //DirectX version level less than 7.0.

                case 0:

                    txtRenderingTier.Text =

                                      string.Format("{0} No hardware acceleration.",

                                                        currentRenderingTier.ToString());       

                    break;

                //DirectX version level greater than 7.0 but less than 9.0.

                case 1:

                    txtRenderingTier.Text =

                                      string.Format("{0} Partial hardware acceleration.",

                                                        currentRenderingTier.ToString());

                                                                                                                                                                                                                 

                    break;

                //DirectX version level greater than or equal to 9.0.

                case 2:

                    txtRenderingTier.Text =

                                      string.Format("{0} Total hardware acceleration.",

                                                        currentRenderingTier.ToString());

                    break;

            }

        }

    }

}

Tip: You can use the Rendering Tier to adjust the user interface. This way you can limit animations and other GPU-intensive operations to the graphics cards that support them.

Note: The code behind looks similar to the code behind of a WinForms form.

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.