Books & ebooks about Microsoft tools, technologies, & research. Plus programming best practices. We hope you enjoy this post.
Here’s a post about the newly published Introducing Windows 7 for Developers, by Yochay Kiriaty, Laurence Moroney, Sasha Goldshtein and Alon Fliess. (The book’s ISBN is 9780735626829, and it contains 416 pages.)
You can read the Foreword by Mark Russinovich here.
Today we’d like to share some other excerpts from the book. First, here is the book’s full Table of Contents:
Table of ContentsForeword..........................................xiiiAcknowledgments ............................xvIntroduction.......................................xvii
1 Welcome to Windows 7..................1What Has Changed Since Windows Vista? ........................................................1Seven Ways to Shine on Windows 7................................................................3Taskbar...................................................................................................4Libraries .................................................................................................6Touch, Multitouch, and Gestures...................................................................8Sensor and Location ..................................................................................9Ribbon ...............................................................................................................10Enhanced Graphics Platform............................................................................ 13Improved Fundamentals ................................................................................... 15Summary........................................................................................................ 18
2 Integrate with the Windows 7 Taskbar: Basic Features ................ 19Design Goals of the Windows 7 Taskbar................................................................. 21A Feature Tour of the Windows 7 Taskbar ............................................................. 21Jump Lists .............................................................................................................. 22Taskbar Overlay Icons and Progress Bars ..................................................... 25Thumbnail Toolbars............................................................................................ 26Live Window Thumbnails.................................................................................. 27Backward Compatibility .................................................................................... 29Integrating with the Windows 7 Taskbar................................................................ 30Application ID....................................................................................................... 30Taskbar Progress Bars and Overlay Icons ..................................................... 35Summary...................................................................................................... 40
3 Integrate with the Windows 7 Taskbar: Advanced Features..............43Jump Lists ................................................................................................... 43Anatomy of a Jump List..................................................................................... 44Recent and Frequent Destinations ................................................................. 45Custom Destinations........................................................................................... 49User Tasks .............................................................................................................. 53Thumbnail Toolbars................................................................................................ 56Customizing Thumbnails................................................................................... 59Thumbnail Clipping ............................................................................................ 61Custom Thumbnails ............................................................................................ 62Custom Live Previews.................................................................................................... 65Window Switchers.......................................................................................................... 66Summary.................................................................................................................. 71
4 Organize My Data: Libraries in Windows 7.......................................73Windows Explorer ............................................................................................ 73Changes Made to Windows Explorer in Windows 7................................. 75Welcome to Libraries ........................................................................................ 77Libraries under the Hood ................................................................................. 79Working with Libraries....................................................................................... 84Summary............................................................................................100
5 Touch Me Now: An Introduction to Multitouch Programming.............. 101Multitouch in Windows 7 ...................................................................101Windows 7 Multitouch Programming Models..................................................104The Good Model: Supporting Legacy Applications ................................104The Better Model: Enhancing the Touch Experience..............................105The Best Model: Experience Optimized for Multitouch ........................106How Multitouch Works in Windows 7...................................................................106Architecture Overview: Messages Data Flow ............................................107Supporting Legacy Applications ...................................................................108Working with Gestures ...............................................................................110Handling the WM_GESTURE Message.........................................................111Use the Pan Gesture to Move an Object ....................................................113Use the Zoom Gesture to Scale an Object .................................................115Use the Rotate Gesture to Turn an Object.................................................117Use a Two-Finger Tap to Mimic a Mouse Click ........................................119Use the Press-and-Tap Gesture to Mimic a Mouse Right-Click ..........121Configuring Windows 7 Gestures.................................................................121Summary.............................................................................................124
6 Touch Me Now: Advanced Multitouch Programming................127Working with Raw Touch Messages..........................................................127Setting Up Windows for Touch.....................................................................128Unpacking WM_TOUCH Messages ..............................................................129Using the Manipulation and Inertia Engines .......................................................135Multitouch Architecture: The Complete Picture, Part 1 ........................136Using Manipulation ..........................................................................................138Using Inertia........................................................................................................144Multitouch Architecture: The Complete Picture, Part 2 ........................145Summary...............................................................................................151
7 Building Multitouch Applications in Managed Code..................153Building Your First Touch-Sensitive Application ................................................153Using Windows 7 Touch to Move an Object .......................................................155Using Windows 7 Touch to Scale an Object ........................................................158Using Windows 7 Touch to Rotate an Object......................................................160Using Inertia with Gestures ...........................................................................161Extending for Multiple Objects................................................................................164Building a Gesture-Enabled Picture Control ........................................................165Using the Gesture-Enabled Picture Control.........................................................168Classes to Support Touch and Gestures ...................................................170UIElement Additions.........................................................................................171Summary....................................................................................................172
8 Using Windows 7 Touch with Silverlight ........................................173Introducing Silverlight........................................................................173Creating Your First Silverlight Application ...........................................................176Building Out-of-Browser Applications in Silverlight ..............................179Using the Silverlight InkPresenter Control ...........................................................183An Example of Ink Annotation in Silverlight.............................................184Silverlight Ink Classes for JavaScript Programmers.................................185Programming for Ink in Silverlight ..............................................................189Using the Touch APIs in Silverlight.........................................................195Expanding the Application for Multitouch................................................196Summary...................................................................................................200
9 Introduction to the Sensor and Location Platform ..................... 201Why Sensors?.........................................................................................................201A Word on Security...........................................................................................203Architecture of the Sensor and Location Platform.............................................204What Is a Sensor?..........................................................................................205Working with Sensors .................................................................................................207Integrating Sensors into Your Application ................................................207Discovering Sensors ..........................................................................................207Requesting Sensor Permissions.....................................................................213Interacting with Sensors ..................................................................................218Reading Sensor Data Using Managed Code..............................................227Ambient Light Sensor Application ...............................................................230Summary...........................................................................................232
10 Tell Me Where I Am: Location-Aware Applications .................... 233Why Location Awareness Is So Important ............................................................233Location Platform Architecture................................................................................234Location Devices Are Regular Windows 7 Sensors..................................237Location Information Is Sensitive Information .........................................238Working with the Location API................................................................................239Understanding How the Location API Works ...........................................239Requesting Location Permissions .................................................................242Interacting with the Location Interface ......................................................244Putting It All Together.....................................................................................254Writing a Location-Aware Application Using .NET ............................................255Reading Location Reports and Handling Location Events ....................257Using the Enhanced Default Location Provider Tool for Testing........259Summary 260
11 Develop with the Windows Ribbon, Part 1 ................................... 263History .................................................................................................................263Using the Ribbon............................................................................................267Programming with the Windows Ribbon Framework ......................................273Ribbon Markup ..................................................................................................275Summary............................................................................................................307
12 Develop with the Windows Ribbon, Part 2 ....................................309Programming the Ribbon..........................................................................................309The Minimal Ribbon Revisited.......................................................................310Initialization Phase ............................................................................................316Handling Ribbon Callbacks.............................................................................318The Property System ........................................................................................320Setting Properties Directly or Indirectly.....................................................322Controlling Controls .........................................................................................325Setting Application Mode, and Showing Contextual Tabs and Pop-Ups ..........347Summary.....................................................................................................353
13 Rediscover the Fundamentals: It’s All About Performance .......355Instrumentation and Diagnostics ............................................................................356Performance Counters .....................................................................................356Windows Management Instrumentation...................................................362Event Tracing for Windows ............................................................................365Windows Performance Toolkit ......................................................................365Troubleshooting Platform ..............................................................................369Performance and Efficiency.......................................................................................371Background Services and Trigger Start Services ......................................372Power Management .........................................................................................378Summary.........................................................................................................382
Index.................................................................................................383
And here’s a stretch of text from Chapter 7:
Chapter 7Building Multitouch Applications in Managed Code
In Chapters 5 and 6, you looked at touch-based applications—in particular, the new multitouchcapabilities that are available in the Windows 7 operating system—and how to programapplications to take advantage of Windows 7 multitouch features.
Perhaps you’re not really that into writing in C++, or perhaps you already have applicationsthat are written in managed code and would like to be able to use them with multitouch. Thischapter will look at how you can build applications with multitouch in mind using the Microsoft.NET Framework and, in particular, the Windows Presentation Foundation (WPF)version 4.
Instead of making a dry run through the features, you’re going to get hands-on experienceright away and learn by doing. So fire up Microsoft Visual Studio 2010 and let’s get going.
Building Your First Touch-Sensitive Application
Visual Studio 2010 supports the ability to create WPF Windows applications. You can do thisfrom the File menu. Select New Project, and the New Project Types dialog box opens. Makesure that you’ve selected .NET Framework 4.0 as the target framework (on the top right side),and select WPF Application from the available templates.
Visual Studio creates a basic WPF client application for you containing a couple of XAML files:App.xaml, which contains the application definition, and MainWindow.xaml, which defines theapplication UI. You’ll edit MainWindow.xaml in this section.
Let’s take a look at the basic XAML that is created for you in Window1.xaml:
<Window x:Class="Chapter7_Sample1.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="300" Width="300"><Grid></Grid></Window>
This example defines the window and the code that supports it. In this case, you can see thatthe class that implements this code is Chapter7Sample1.Window1. This code resides in thecode-behind file called MainWindow.xaml.cs. We’ll look at that in a moment.
The attributes on the Window element define the namespaces for the XAML code so that theXAML can be validated by the Visual Studio compiler. Finally, some basic properties such asTitle, Height, and Width are set.
XAML in WPF supports a number of controls that allow you to command how the user interfaceis laid out. The Grid control is one of these; it allows you to define how your controls arelaid out within defined cells. These controls can be placed anywhere within any of the cells. Ifthe Grid control is used, as it is here, without cells (that is, defined using rows and columns),you should consider it a single-cell grid where only one control will be displayed.
Let’s add a control to this grid. We’ll add a rectangle in it, like this:
<Rectangle Width="100" Height="100" Fill="Red"></Rectangle>
This defines a 100 by 100 rectangle (also known as a square), which will appear centeredwithin the single default cell of the grid. You can see the result in the following screen shot:
If you’re thinking that this isn’t very interesting, you’re right. So let’s make it a little more interestingby making it touch sensitive. You can manipulate an object using touch in Windows7 in a number of ways, but we’ll start with the simplest—moving it around the screen.
Using Windows 7 Touch to Move an Object
In the previous section, you built a (very) simple Windows 7 WPF application that rendered ared rectangle on the screen. Now you’re going to add the first step of basic touch input byadding the code that allows you to move the rectangle around the screen using touch.
First you need to inform Windows that you want to be able to manipulate the rectangle usingthe touch system. You do this in XAML with the IsManipulationEnabled attribute. This is aBoolean value, and should be set to ‘true’ if you want to manipulate an objectusing multi touch.
<Rectangle Width="100" Height="100" Fill="Red" IsManipulationEnabled="True" ></Rectangle>
You’re going to make one more change to the rectangle, and that is to add a named Render-Transform object that is defined by a MatrixTransform that does nothing. You’ll see shortlythat this transform can be manipulated to provide the drawing functionality that changes theshape or location of the rectangle upon user input. The MatrixTransform object is defined asan application resource, so if you look at your App.xaml file, you’ll see a section for resources(called Application.Resources), where you can add the MatrixTransform object.
Here’s the code:
<Application.Resources><MatrixTransform x:Key="InitialMatrixTransform"><MatrixTransform.Matrix><Matrix OffsetX="0" OffsetY="0"/></MatrixTransform.Matrix></MatrixTransform></Application.Resources>
As you can see, this matrix transform doesn’t do anything. If you apply it to the rectangle,nothing happens. However, later in the code example, you’ll manipulate the transform, and it,in turn, will manipulate the rectangle.
Here’s how the rectangle XAML should look with the transform applied:
<Rectangle Width="100" Height="100" Fill="Red" ManipulationMode="All"RenderTransform="{StaticResource InitialMatrixTransform}"></Rectangle>
Now that the rectangle is configured to allow manipulation, the next step is to set up whatthe window needs to capture manipulation events from the operating system. These are capturedusing the ManipulationDelta event. Add this, and specify a handler to the Window tagat the root of the XAML for Window1.xaml.
It should look something like this:
<Window x:Class="Chapter7_Sample1.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="Window1" Height="300" Width="300" ManipulationDelta="Window_ManipulationDelta">
When you created the ManipulationDelta event handler on the Window just shown, VisualStudio should have created a stub function for you. If it didn’t (for example, if you cut andpasted the attribute declaration instead of typing it), you can see it here:
private void Window_ManipulationDelta(object sender, ManipulationDeltaEventArgs e){}
If you’re familiar with .NET programming, this code will look familiar. It’s a typical event handlerthat takes a general object called sender, which refers to the object that the event wasraised on, and an object containing the event arguments (that is, the metadata associatedwith the event).
In this case, you get a ManipulationDeltaEventArgs object. This object exposes a functioncalled GetDeltaManipulation, which returns a Manipulation object that contains exactly themetadata that you need to understand what the user has done to the object.
Here’s how you can get a handle on the manipulation that has just been performed:
ManipulationDelta m = e.DeltaManipulation;
Next, you want to get a reference to your rectangle, and to the Matrix that defines its Render-Transform. Here’s the code:
Rectangle r = e.OriginalSource as Rectangle;Matrix matrix = ((MatrixTransform)r.RenderTransform).Matrix;
The Manipulation object exposes a Translation property that returns a Vector object, whichhas member properties for the translation on X and Y that results from the user’s action ofmoving the object. To move the object, you should call the Translate method on the matrix.(Remember from the code shown earlier, that the matrix here is the RenderTransform fromthe rectangle.) Here’s the relevant code:
matrix.Translate(m.Translation.X, m.Translation.Y);
And now all you need to do is set the RenderTransform for the rectangle to be the new matrix(that is, the one that has been manipulated by the Translation function):
r.RenderTransform = new MatrixTransform(matrix);
And that’s it. Now, all you have to do is implement this event handler and you can move therectangle based on user input from the touch screen! Here’s the full event handler:
private void Window_ManipulationDelta(object sender, ManipulationDeltaEventArgs e){Manipulation m = e.GetDeltaManipulation(this);Rectangle r = e.OriginalSource as Rectangle;Matrix matrix = ((MatrixTransform)r.RenderTransform).Matrix;matrix.Translate(m.Translation.X, m.Translation.Y);r.RenderTransform = new MatrixTransform(matrix);}
Now if you run your application, you can move the rectangle around the window by touchingthe screen with your finger! Here’s an illustration of how it works:
In the next section, you add to this to see how the multitouch system accepts scaling informationthat allows you to change the size of the object.
> > >
Microsoft is interested in hearing your feedback so that we can continually improve our books and learningresources for you. To participate in a brief online (and anonymous) survey, please visit
www.microsoft.com/learning/booksurvey/
Enjoy the book!