The Windows 7 SDK comes with several components, including tools, documentation, headers and libraries, and samples. In this series of blogs, I’ll go through a number of sample that particularly target the new features in Windows 7. I’ll talk about how to build and run them and what the requirements are for each.

Samples have been one of the most valuable educational tools out there and Windows SDK sure has all those samples needed to educate yourself about any aspect of Windows programming.

For the rest of this article, I’ll assume the default location for the Windows 7 SDK root folder, which is “C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples”

Direct2D and DirectWrite samples

New to Windows 7 are Direct2D and Direct3D, which are part of the DirectX family. These technologies are aimed at providing a better and faster developer and user experience for creating 2D graphics and text

Direct2D (or D2D) is a native immediate-mode 2D graphics rendering API that is built on top of Direct3D, and offers some compelling performance and visual quality improvements over GDI and GDI+. DirectWrite supports high-quality text rendering, resolution-independent outline fonts, and full Unicode text and layout support.

I’m not going to go into detail about these technologies now. But, you can always Bing them or directly visit the MSDN portals for Direct2d and DirectWrite, where you’ll find a wealth of information about each and how they interoperate together or with their other family member, Dirct3D.

Samples included in the Windows 7 SDK

The samples that illustrate Direct2D are installed by default under “C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\multimedia\Direct2D”. Similarly, the DirectWrite samples are installed to “C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\multimedia\DirectWrite”. (The blog post New Win32 Samples in Windows SDK for Windows 7: Multimedia describes all multimedia samples in native code that were updated for Windows 7.)

All of the Direct2D and DirectWrite samples come preconfigured with Visual Studio 2008 solution and project files.

Other than Visual Studio 2008, and the Win32 headers and libraries that are installed with the Windows SDK, the only prerequisite you might need is DirectX SDK. It is needed for only two Direct3D samples that require a compiler shader that ships with DirectX SDK. Both samples interop with D3D. (See the blog post Development with Windows SDK & DirectX SDK when Visual Studio 2008 is installed for more information on working with both the Windows SDK and the DirectX SDK.)

Here are the available samples.

Direct2D Samples (C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\

Multimedia\Direct2D):

Sample Description Location
Simple "Hello, World" Direct2D application that draws text to the screen. Demonstrates how to use Direct2D to draw the text, “Hello, World.”

Direct2DHelloWorld

Direct3D/Direct2D Interoperability Sample

Demonstrates how to use Direct2D to project 2D content onto a 3D surface.

DXGI Interoperation Sample

GDI/Direct2D Interoperability Sample

Demonstrates how to write Direct2D content to a GDI DC.

GdiInteropSample

Direct2D Geometry Realization Sample

Demonstrates how to use meshes and A8 render targets to improve performance when rendering complex shapes.

GeometryRealizationSample

Interactive 3D Text Sample

Demonstrates how to use DirectWrite and Direct2D to create editable 3D text.

Interactive3dTextSample

Direct2D ListView Control Sample

Demonstrates how to use Direct2D to create a list view control.

ListViewSample

Direct2D Antialiasing Sample

Demonstrates different antialiasing rendering modes.

MSAARenderingSample

Direct2D Saving an Image as a File Example

Demonstrates how to write Direct2D content to an image file.

SaveAsImageFileSample

Simple Direct2D Application

Demonstrates a simple Direct2D application. It draws text, images, shapes, and creates a pattern.

SimpleDirect2DApplication

Direct2D Animate Along a Path Sample

Demonstrates how to animate an object along a geometric path.

SimplePathAnimationSample

Direct2D Text Animation Sample

Demonstrates different ways to render animated text.

TextAnimationSample

DirectWrite Samples (C:\Program Files\Microsoft SDKs\Windows\v7.0\Samples\Multimedia\DirectWrite):

 

Sample Description Location

A Choose Font Dialog Using DirectWrite

Demonstrates choosing a font face, size and weight.

ChooseFont

Custom Font Loader Sample

Demonstrates how to load a custom font embedded in an application using DirectWrite.

CustomFont

DirectWrite Custom Layout Sample

Demonstrates how a custom layout can utilize the information from script itemization, bidi analysis, line breaking analysis, and shaping to accomplish text measurement and fitting, line breaking, basic justification, and drawing.

CustomLayout

Font Enumeration Sample

Demonstrates how to enumerate fonts using DirectWrite and outputs the font family names to the console.

FontEnumeration

DirectWrite - GDI Interoperation Sample

Demonstrates how DirectWrite can convert from GDI font objects and display DirectWrite text on a GDI surface.

GdiInterop

Hello World using DirectWrite

Demonstrates how to use DirectWrite to render simple text, text with multiple formats, and customized text with a custom renderer.

HelloWorld

DirectWrite layout sample

Demonstrates various DirectWrite features such as: Glyph rendering, Complex script shaping, Script analysis, Bidi ordering (?abc?), Line breaking, Font fallback, Font enumeration, ClearType rendering, OpenType styles, Inline objects, Trimming, and Selection hit-testing.

PadWrite

DirectWrite Text Rendering Sample

Demonstrates the rendering capabilities of DirectWrite, both to a DIB and a Direct2D surface.

RenderTest

A simple version of Hello World with DirectWrite

A version of the DirectWrite Hello World sample that uses simple text only.

SimpleHelloWorld

DirectWrite Text Dialog Sample

Demonstrates font enumeration, changing font face, style, weight, underline, and size using DirectWrite. Displays the changes in real time.

TextDialogSample

Building SDK samples in the “Program Files” directory

When building Samples, you should do so in a directory outside C:\Program Files to which you have write access. Copying to a location other than C:\Program Files makes it possible to maintain a pristine copy of the SDK samples and avoid issues when writing to files and directories located under C:\Program Files.

If you wish to build the samples in the “Program Files” directory, which is a secured Windows folder, Visual Studio 2008 may ask you to run with elevated permissions.  (Click on the images to enlarge them.)

clip_image002

Use the Windows SDK Configuration Tool to set the Current SDK

Building the SDK samples with Visual Studio 2008 should be straightforward after you install Windows SDK and Use the Windows SDK Configuration Tool to set the Windows 7 SDK as the “current” SDK for MSBuild and VCBuild. Running this tool will update the Windows 7 SDK build environment and the Visual Studio 2008 IDE build environment to use the Windows 7 SDK headers, libraries and tools.  You can also use this tool to switch it back again. (For more information, see the blog post Using the Win 7 SDK Build Environment with VS 2008.)

To run the GUI version of the Windows SDK Configuration Tool, go to Start, All Programs, Microsoft Windows SDK v7.0, Visual Studio Registration, Windows SDK Configuration Tool, and set the Windows SDK version to v7.0 for Visual Studio 2008. (Because this tool modifies registry settings, it needs elevated permissions to run.)

clip_image004

After the tool launches, select v7.0 from the drop down menu. (You may see something slightly different on your computer. In the screenshot below, you see two other versions of the SDK. v6.0A indicates the Windows SDK components that ship in-the-box with Visual Studio 2008. 7.0A indicates the Windows SDK components that ship in-the-box with Visual Studio 2010 BETA integrated SDK.)

clip_image006

Click Make Current, and you should see an output like this:

clip_image008

Now, restart Visual Studio 2008 and rebuild the solution and you will be building with the Windows 7 components:

clip_image010

Setting Visual C++ directories manually

You may wish to set the Visual C++ directories manually. However, if you have successfully configured Visual Studio directories using the Windows SDK Configuration tool, you won’t need to follow the manual steps below.

1. From the main Visual Studio menu, select Tools àOptions, and open the Project and Solutions | VC++ Directories property sheet. This is used to select the location of the folders Visual Studio needs to build and run Visual C++ solutions. (The Platform is the build/run target; the most common are Win32 (x86) or x64.)
clip_image012

2. We want to update the Include folders path, so In “Show directories for:”, select Include Files, and add the SDK include path, c:\Program Files\Microsoft SDKs\Windows\v7.0\Include, to the top of the list:
clip_image014

3. In the Libraries files directories, add C:\Program Files\Microsoft SDKs\Windows\v7.0\Lib to the top of the list (Note, the x64 Library Files are under C:\Program Files\Microsoft SDKs\Windows\v7.0\Lib\x64):
clip_image016
Remember, if you have successfully configured Visual Studio folders using WinSDK Configuration tool, you won’t need any of the manual steps above.

Troubleshooting

After building and running the solution, if you get a compilation error such as:” Fatal error C1083: Cannot open include file: 'd2d1.h': No such file or directory”

clip_image018

This means that Visual Studio 2008 cannot find the headers (and probably it won’t find the libraries too). This might happen for several reasons, for example if you had more than one version of Visual Studio when the SDK was installed.

Make sure you have told Visual Studio 2008 about the location of those file by using either the Windows SDK Configuration tool, or setting the VC++ directories manually, using instructions earlier in this post.

Running the samples

Now, back to building and running the Direct2D and DirectWrite samples. Here are some screen shots for the samples.  (Click on the image to enlarge it.)


Simple Direct2D Application:

clip_image020

Direct2D Geometry realization Samples:

clip_image022

Remember, two of the Direct2D samples, DXGISample and Interactive3dTextSample, require that the DirectX SDK is installed on your machine. (See the blog post Development with Windows SDK & DirectX SDK when Visual Studio 2008 is installed for more information on working with both the Windows SDK and the DirectX SDK.)

Here are shots of the Direct2D Interactive 3D Text Sample, which shows the interaction between Direct3D (through DXGI), Direct2D and DirectWrite:

clip_image024

Here is the DirectWrite Simple Hello World sample:

clip_image026

Here is the Render Test Sample:

clip_image028

Here is the Custom layout sample in English and Arabic:

clip_image030

clip_image032

Mahmoud Saleh
Software Engineer
Windows C++ Team