The Windows Presentation Foundation (WPF) is a
graphical display system for Windows. WPF is designed for .NET,
influenced by modern display technologies such as HTML and Flash, and
hardware-accelerated. It's also the most radical change to hit Windows
user interfaces since Windows 95.
1. The Evolution of Windows Graphics
It's hard to appreciate
how dramatic WPF is without realizing that Windows developers have been
using essentially the same display technology for more than 15 years. A
standard Windows application relies on two well-worn parts of the
Windows operating system to create its user interface:
User32. This provides the familiar Windows look and feel for elements such as windows, buttons, text boxes, and so on.
GDI/GDI+.
This provides drawing support for rendering shapes, text, and images at
the cost of additional complexity (and often lackluster performance).
Over the years, both
technologies have been refined, and the APIs that developers use to
interact with them have changed dramatically. But whether you're
crafting an application with .NET and Windows Forms or lingering in the
past with Visual Basic 6 or MFC-based C++ code, behind the scenes the
same parts of the Windows operating system are at work. Newer frameworks
simply deliver better wrappers for interacting with User32 and
GDI/GDI+. They can provide improvements in efficiency, reduce
complexity, and add prebaked features so you don't have to code them
yourself; but they can't remove the fundamental limitations of a system
component that was designed more than a decade ago.
NOTE
The basic division of
labor between User32 and GDI/GDI+ was introduced more than 15 years ago
and was well established in Windows 3.0. Of course, User32 was simply
User at that point, because software hadn't yet entered the 32-bit
world.
1.1. DirectX: The New Graphics Engine
Microsoft created one way around the limitations of the User32 and GDI/GDI+ libraries: DirectX.
DirectX began as a cobbled-together, error-prone toolkit for creating
games on the Windows platform. Its design mandate was speed, and so
Microsoft worked closely with video card vendors to give DirectX the
hardware acceleration needed for complex textures, special effects such
as partial transparency, and three-dimensional graphics.
Over the years since it was
first introduced (shortly after Windows 95), DirectX has matured. It's
now an integral part of Windows, with support for all modern video
cards. However, the programming API for DirectX still reflects its roots
as a game developer's toolkit. Because of its raw complexity, DirectX
is almost never used in traditional types of Windows applications (such
as business software).
WPF changes all this. In WPF,
the underlying graphics technology isn't GDI/GDI+. Instead, it's
DirectX. Remarkably, WPF applications use DirectX no matter what type of
user interface you create. That means that whether you're designing
complex three-dimensional graphics (DirectX's forte) or just drawing
buttons and plain text, all the drawing work travels through the DirectX
pipeline. As a result, even the most mundane business applications can
use rich effects such as transparency and anti-aliasing. You also
benefit from hardware acceleration, which simply means DirectX hands off
as much work as possible to the graphics processing unit (GPU), which
is the dedicated processor on the video card.
NOTE
DirectX is more
efficient because it understands higher-level ingredients such as
textures and gradients that can be rendered directly by the video card.
GDI/GDI+ doesn't, so it needs to convert them to pixel-by-pixel
instructions, which are rendered much more slowly by modern video cards.
One component that's still
in the picture (to a limited extent) is User32. That's because WPF still
relies on User32 for certain services, such as handling and routing
input and sorting out which application owns which portion of screen
real estate. However, all the drawing is funneled through DirectX.
NOTE
This is the most significant change in WPF. WPF is not a wrapper for GDI/GDI+. Instead, it's a replacement—a separate layer that works through DirectX.
1.2. Hardware Acceleration and WPF
You're probably aware that
video cards differ in their support for specialized rendering features
and optimizations. Fortunately, this isn't a problem, because WPF has
the ability to perform everything it does using software calculations
rather than relying on built-in support from the video card.
NOTE
There's one exception to
WPF's software support. Because of poor driver support, WPF performs
anti-aliasing for 3-D drawings only if you're running your application
on Windows Vista or Windows 7 (and you have a native WDDM driver for
your video card). That means that if you draw three-dimensional shapes
on a Windows XP computer, you'll end up with slightly jagged edges
rather than nicely smoothed lines. However, anti-aliasing is always
provided for 2-D drawings, regardless of the operating system and driver
support.
Having a
high-powered video card is not an absolute guarantee that you'll get
fast, hardware-accelerated performance in WPF. Software also plays a
significant role. For example, WPF can't provide hardware acceleration
to video cards that are using out-of-date drivers. (If you're using an
older video card, these out-of-date drivers are quite possibly the only
ones that were provided in the retail package.) WPF also provides better
performance under the Windows Vista and Windows 7 operating systems,
where it can take advantage of the Windows Display Driver Model (WDDM).
WDDM offers several important enhancements beyond the Windows XP Display
Driver Model (XPDM). Most importantly, WDDM allows several GPU
operations to be scheduled at once, and it allows video card memory to
be paged to normal system memory if you exceed what's available on the
video card.
As a general rule of thumb, WPF
offers some sort of hardware acceleration to all WDDM drivers and to
XPDM drivers that were created after November 2004, which is when
Microsoft released new driver development guidelines. Of course, the
level of support differs. When the WPF infrastructure first starts up,
it evaluates your video card and assigns it a rating from 0 to 2, as
described in the sidebar "WPF Tiers."
Part of the promise of WPF
is that you don't need to worry about the details and idiosyncrasies of
specific hardware. WPF is intelligent enough to use hardware
optimizations where possible, but it has a software fallback for
everything. So if you run a WPF application on a computer with a legacy
video card, the interface will still appear the way you designed it. Of
course, the software alternative may be much slower, so you'll find that
computers with older video cards won't run rich WPF applications very
well, especially ones that incorporate complex animations or other
intense graphical effects. In practice, you might choose to scale down
complex effects in the user interface, depending on the level of
hardware acceleration that's available in the client (as indicated by
theRenderCapability.Tier property).
NOTE
The goal of WPF is to off-load as much of the work as possible on the video card so that complex graphics routines are render-bound (limited by the GPU) rather than processor-bound
(limited by your computer's CPU). That way, you keep the CPU free for
other work, you make the best use of your video card, and you are able
to take advantage of performance increases in newer video cards as they
become available.
Video cards differ
significantly. When WPF assesses a video card, it considers a number of
factors, including the amount of RAM on the video card, support for
pixel shaders (built-in routines that calculate per-pixel effects such
as transparency), and support for vertex shaders (built-in routines that
calculate values at the vertexes of a triangle, such as the shading of a
3-D object). Based on these details, it assigns a rendering tier value.
WPF recognizes three rendering tiers:
Rendering Tier 0. The video card will not provide any hardware acceleration. This corresponds to a DirectX version level of less than 7.0.
Rendering Tier 1.
The video card can provide partial hardware acceleration. This
corresponds to a DirectX version level greater than 7.0 but less than
9.0.
Rendering Tier 2.
All features that can be hardware accelerated will be. This corresponds
to a DirectX version level greater than or equal to 9.0.
In some situations, you might
want to examine the current rendering tier programmatically so you can
selectively disable graphics-intensive features on lesser-powered cards.
To do so, you need to use the static Tier property of the
System.Windows.Media.RenderCapability class. But there's one trick. To
extract the tier value from the Tier property, you need to shift it 16
bits, as shown here:
int renderingTier = (RenderCapability.Tier >> 16);
if (renderingTier == 0)
{ ... }
else if (renderingTier == 1)
{ ... }
This design allows
extensibility. In future versions of WPF, the other bits in the Tier
property might be used to store information about support for other
features, thereby creating subtiers.
For more information about
what WPF features are hardware-accelerated for tier 1 and tier 2 and for
a list of common tier 1 and tier 2 video cards, refer to http://msdn.microsoft.com/en-us/library/ms742196(VS.100).aspx.
2. WPF: A Higher-Level API
If the only thing WPF offered
was hardware acceleration through DirectX, it would be a compelling
improvement but not a revolutionary one. But WPF actually includes a
basket of high-level services designed for application programmers.
The following are some of the most dramatic changes that WPF ushers into the Windows programming world:
A web-like layout model.
Rather than fix controls in place with specific coordinates, WPF
emphasizes flexible flow layout that arranges controls based on their
content. The result is a user interface that can adapt to show highly
dynamic content or different languages.
A rich drawing model. Rather than painting pixels, in WPF you deal with primitives—basic
shapes, blocks of text, and other graphical ingredients. You also have
new features, such as true transparent controls, the ability to stack
multiple layers with different opacities, and native 3-D support.
A rich text model.
After years of substandard text handling, WPF finally gives Windows
applications the ability to display rich, styled text anywhere in a user
interface. You can even combine text with lists, floating figures, and
other user interface elements. And if you need to display large amounts
of text, you can use advanced document display features such as
wrapping, columns, and justification to improve readability.
Animation as a first-class programming concept.
In WPF, there's no need to use a timer to force a form to repaint
itself. Instead, animation is an intrinsic part of the framework. You
define animations with declarative tags, and WPF puts them into action
automatically.
Support for audio and video media.
Previous user interface toolkits, such as Windows Forms, were
surprisingly limited when dealing with multimedia. But WPF includes
support for playing any audio or video file supported by Windows Media
Player, and it allows you to play more than one media file at once. Even
more impressively, it gives you the tools to integrate video content
into the rest of your user interface, allowing you to pull off exotic
tricks such as placing a video window on a spinning 3-D cube.
Styles and templates.
Styles allow you to standardize formatting and reuse it throughout your
application. Templates allow you to change the way any element is
rendered, even a core control such as the button. It's never been easier
to build modern skinned interfaces.
Commands.
Most users realize that it doesn't matter whether they trigger the Open
command through a menu or through a toolbar; the end result is the
same. Now that abstraction is available to your code, you can define an
application command in one place and link it to multiple controls.
Declarative user interface.
Although you can construct a WPF window with code, Visual Studio takes a
different approach. It serializes each window's content to a set of XML
tags in a XAML document. The advantage is that your user interface is
completely separated from your code, and graphic designers can use
professional tools to edit your XAML files and refine your application's
front end.
Page-based applications.
Using WPF, you can build a browser-like application that lets you move
through a collection of pages, complete with forward and back navigation
buttons. WPF handles the messy details such as the page history. You
can even deploy your project as a browser-based application that runs
right inside Internet Explorer.
2.1. Windows Forms Lives On
WPF is the platform for the
future of Windows user interface development. However, it won't displace
Windows Forms overnight. Windows Forms is in many ways the culmination
of the previous generation of display technology, which was built on
GDI/GDI+ and User32.
So, which platform should
you choose when you begin designing a new Windows application? If you're
starting from the ground up, WPF is an ideal choice, and it offers the
best prospects for future enhancements and longevity. Similarly, if you
need one of the features that WPF provides and Windows Forms does
not—such as 3-D drawing or page-based applications—it makes sense to
make the shift. On the other hand, if you have a considerable investment
in a Windows Forms–based business application, there's no need to
recode your application for WPF. The Windows Forms platform will
continue to be supported for years to come.
Perhaps the best part of the
story is that Microsoft has invested considerable effort in building an
interoperability layer between WPF and Windows Forms (which plays a
similar role to the interoperability layer that allows .NET applications
to continue to use legacy COM components).
2.2. DirectX Also Lives On
There's one area where WPF isn't
a good fit—when creating applications with demanding real-time
graphics, such as complex physics-based simulators or cutting-edge
action games. If you want the best possible video performance for these
types of applications, you'll need to program at a much lower level and
use raw DirectX. You can download the managed .NET libraries for DirectX
programming at http://msdn.microsoft.com/directx.
2.3. Silverlight
Like the .NET Framework, WPF is a Windows-centric technology.
That means that WPF applications can be used only on computers running
the Windows operating system. Browser-based WPF applications are
similarly limited—they can run only on Windows computers, although they
support both the Internet Explorer and Firefox browsers.
These restrictions won't
change—after all, part of Microsoft's goal with WPF is to take advantage
of the rich capabilities of Windows computers and its investment in
technologies such as DirectX. However, Silverlight is designed to take a
subset of the WPF platform, host it in any modern browser using a
plug-in (including Firefox, Google Chrome, and Safari), and open it up
to other operating systems (such as Linux and Mac OS). This is an
ambitious project that's attracted considerable developer interest.
In many ways, Silverlight is
based on WPF, and it incorporates many of WPF's conventions . However,
Silverlight also leaves out certain feature areas, such as true
three-dimensional drawing or rich document display. New features may
appear in future Silverlight releases, but the more complex ones might
never make the leap.
The ultimate goal of
Silverlight is to provide a powerful developer-oriented competitor for
Adobe Flash. However, Flash has a key advantage—it's used throughout the
Web, and the Flash plug-in is installed just about everywhere. To
entice developers to switch to a new, less-established technology,
Microsoft will need to make sure Silverlight has next-generation
features, rock-solid compatibility, and unrivaled design support.