In my last post Windows 7 Multitouch Application Development (Part - I), I described how to handle multitouch image manipulation in Windows 7 which gives a very basic idea on the multitouch development. That code used multitouch manipulation for the entire screen. If there are multiple images in the screen, this will raise events for all of them.
In this post, I will show you how to manage multitouch events for all the images separately. See one such image manipulation demo here.
For this, we have to assign a unique touch-id for each finger on the screen. As long as the finger touches the screen, the associated touch-id will remain the same for that particular finger. If the user releases his finger, the system will release the touch-id and that can be again assigned by the system automatically on next touch. So, how can we get the touch-id? You can get it from the StylusEventArgs
(i.e., args.StylusDevice.Id
). The stylus device will automatically generate this ID for each touch; the only thing is you have to assign it with the respective finger touch.
First of all, we will create a User Control which will consist of a single image and the XAML code for its RenderTransform
. This is the same thing we did in the previous post which was inside the Window
, but here, it will be inside the User Control (Picture
class). Create a DependencyProperty
to assign the ImageLocation
dynamically.
<UserControl x:Class="Windows7MultitouchDemo.Picture"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Image Source="{Binding Path=ImageLocation}" Stretch="Fill" Width="Auto"
Height="Auto" RenderTransformOrigin="0.5, 0.5">
<Image.RenderTransform>
<TransformGroup>
<RotateTransform x:Name="trRotate"/>
<ScaleTransform x:Name="trScale"/>
<TranslateTransform x:Name="trTranslate"/>
</TransformGroup>
</Image.RenderTransform>
</Image>
</UserControl>
To track the multi-touch simultaneously for the above “Picture
” user control, you can use the PictureTracker
class which comes with the Windows 7 Training Kit. You can download it from the Microsoft website. It looks similar to this:
public class PictureTracker
{
private Point _prevLocation;
public Picture Picture { get; set; }
public void ProcessDown(Point location)
{
_prevLocation = location;
}
public void ProcessMove(Point location)
{
Picture.X += location.X - _prevLocation.X;
Picture.Y += location.Y - _prevLocation.Y;
_prevLocation = location;
}
public void ProcessUp(Point location)
{
}
}
Now, we have to store all the active touch-ids associated with the PictureTracker
class. So, we will use a Dictionary
for that. We will use the same PictureTrackerManager
class which again comes with the Windows 7 Training Kit.
private readonly Dictionary<int, PictureTracker> _pictureTrackerMap;
Create an instance of the PictureTrackerManager
class inside your Window1.xaml.cs and register the stylus events with the PictureTrackerManager
events. So now, whenever a touch occurs on the Picture
, the PictureTrackerManager
will first find the associated touch-id for the respective instance and raise an event to process the same.
StylusDown += _pictureTrackerManager.ProcessDown;
StylusUp += _pictureTrackerManager.ProcessUp;
StylusMove += _pictureTrackerManager.ProcessMove;
Reference