.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Articles >> WPF >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Windows 7 Multitouch Application Development (Part - I)

Posted By:Kunal Chowdhury       Posted Date: March 06, 2010    Points: 25    Category: WPF    URL: http://www.dotnetspark.com  

Windows 7 came up with lots of goodies including better resource management, better performance, jumplist management, multitouch functionality & many more. Here I will discuss on developing a simple multitouch application using .Net 3.5 SP1.

Windows 7 came up with lots of goodies including better resource management, better performance, jumplist management, multitouch functionality & many more. Here I will discuss on developing a simple multitouch application using .Net 3.5 SP1. 

Before doing anything you have to download the Windows 7 Multitouch API. You can download it from here. Extract the downloaded zip file to your local hard drive. Be sure that, you are using Windows 7 & you have a multitouch enabled screen to test it out. 

XAML Implementation

Create a WPF application using Visual Studio 2008. This will automatically add a XAML file named Window1.xaml for you. Now add an image to your solution directory & insert it in the XAML. Now your Window1.xaml will look something like this: 


Add RenderTransform to the image so that, we can scale or rotate the image properly. This will produce XAML similar to this: 

Image Source="images/Hydrangeas.jpg" RenderTransformOrigin="0.5,0.5" Width="400">
ScaleTransform x:Name="trScale" ScaleX="1" ScaleY="1"/>
RotateTransform x:Name="trRotate" Angle="0"/>
TranslateTransform x:Name="trTranslate" X="0" Y="0"/>
SkewTransform AngleX="0" AngleY="0"/>

Use proper names when you are adding different types of transform to the transform group. It will be easier for you to handle it from the code behind file. Run your application. This will open up your Window with an image inside it. If you want to drag or rotate the image this will not work because we haven't integrated the functionality yet.

Code Implementation

Add two project references i.e. "Windows7.Multitouch" & "Windows7.Multitouch.WPF" from the extracted zip folder to your solution. These are the managed API codes for multitouch application development. Go to your Window1.xaml.cs & be sure that following namespaces are already included. You may have to add some of them. 
using System; 
using System.Windows;
using Windows7.Multitouch;
using Windows7.Multitouch.Manipulation;
using Windows7.Multitouch.WPF;

Create two private members inside your partial class: 

// object of a .Net Wrapper class for processing multitouch manipulation 
private ManipulationProcessor manipulationProcessor = new ManipulationProcessor(ProcessorManipulations.ALL);

// boolean value to check whether you have a multitouch enabled screen
private static bool IsMultitouchEnabled = TouchHandler.DigitizerCapabilities.IsMultiTouchReady;

Now inside the Window Loaded event write the following lines of code: 

// check to see whether multitouch is enabled 
if (IsMultitouchEnabled)
// enables stylus events for processor manipulation

// add the stylus events
StylusDown += (s, e) =>
manipulationProcessor.ProcessDown((uint)e.StylusDevice.Id, e.GetPosition(this).ToDrawingPointF());
StylusUp += (s, e) =>
manipulationProcessor.ProcessUp((uint)e.StylusDevice.Id, e.GetPosition(this).ToDrawingPointF());
StylusMove += (s, e) =>
manipulationProcessor.ProcessMove((uint)e.StylusDevice.Id, e.GetPosition(this).ToDrawingPointF());

// register the ManipulationDelta event with the manipulation processor
manipulationProcessor.ManipulationDelta += ProcessManipulationDelta;

// set the rotation angle for single finger manipulation
manipulationProcessor.PivotRadius = 2;

Write your logic inside the manipulation event handler implementation block. Here I will do rotation, scaling & positioning of the image. Here is my code: 

private void ProcessManipulationDelta(object sender, ManipulationDeltaEventArgs e)
trTranslate.X += e.TranslationDelta.Width;
trTranslate.Y += e.TranslationDelta.Height;

trRotate.Angle += e.RotationDelta * 180 / Math.PI;

trScale.ScaleX *= e.ScaleDelta;
trScale.ScaleY *= e.ScaleDelta;

From the ManipulationDeltaEventArgs you can get various values & depending upon them you can implement your functionality in this block. TranslateTransform will position the image, RotateTransform will do the rotation & the ScaleTransform will resize the image. Run your project to test your first multitouch application. 

Download Sample Application 

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend