Using Windows 7 Touch with Silverlight in Visual Basic .NET

Encoding QR Code in Visual Basic .NET Using Windows 7 Touch with Silverlight

8 Using Windows 7 Touch with Silverlight
Paint QR Code In VB.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create QR Code image in VS .NET applications.
www.OnBarcode.com
QR Code ISO/IEC18004 Decoder In VB.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
In the next section, you ll look at the raw touch APIs provided in Silverlight 3, and you ll see how these can be used to build Touch applications in particular, multitouch applications running on Windows 7.
Bar Code Maker In VB.NET
Using Barcode encoder for .NET framework Control to generate, create barcode image in .NET applications.
www.OnBarcode.com
Read Barcode In VB.NET
Using Barcode reader for .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Using the Touch APIs in Silverlight
Drawing Denso QR Bar Code In C#.NET
Using Barcode drawer for VS .NET Control to generate, create QR-Code image in VS .NET applications.
www.OnBarcode.com
QR Code JIS X 0510 Generation In .NET
Using Barcode drawer for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
www.OnBarcode.com
Silverlight 3 introduces a new Touch object that can be used to build single-touch or multitouch applications, depending on your touch hardware. Using it is straightforward. To get started, you simply write an event handler for its FrameReported event, and then handle the touch arguments within this event handler. These arguments vary depending on your touch hardware and where you touch. Let s first look at an example using a Tablet PC with a touch screen, a common piece of hardware that provides a single touch interface. Create a new Silverlight application, and change its MainPage.xaml to look like the following code sample. Note that you are adding an Ellipse called finger as well as changing the Grid to a Canvas. Here s the XAML:
QR Code Drawer In Visual Studio .NET
Using Barcode maker for .NET framework Control to generate, create QR Code 2d barcode image in .NET applications.
www.OnBarcode.com
Quick Response Code Printer In VB.NET
Using Barcode generation for .NET Control to generate, create QR Code image in .NET applications.
www.OnBarcode.com
<UserControl x:Class="SecondTouch.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"> <Canvas x:Name="LayoutRoot"> <Ellipse Width="60" Height="60" Fill="Red" x:Name="finger"></Ellipse> </Canvas> </UserControl>
ECC200 Creation In Visual Basic .NET
Using Barcode creation for Visual Studio .NET Control to generate, create ECC200 image in .NET applications.
www.OnBarcode.com
Printing Linear In VB.NET
Using Barcode maker for Visual Studio .NET Control to generate, create Linear Barcode image in VS .NET applications.
www.OnBarcode.com
Next, go to the MainPage.xaml.cs file and look for the class constructor. Add the definition of the event handler for the FrameReported event on the Touch object. Your code will look like this:
Creating Code 128 Code Set B In Visual Basic .NET
Using Barcode printer for VS .NET Control to generate, create Code 128 Code Set B image in .NET applications.
www.OnBarcode.com
UCC - 14 Encoder In VB.NET
Using Barcode creator for Visual Studio .NET Control to generate, create DUN - 14 image in .NET applications.
www.OnBarcode.com
public MainPage() { InitializeComponent(); Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported); }
Barcode Creation In None
Using Barcode encoder for Font Control to generate, create barcode image in Font applications.
www.OnBarcode.com
Drawing Linear Barcode In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create 1D Barcode image in ASP.NET applications.
www.OnBarcode.com
After you create the event handler declaration, Visual Studio creates the event handler code for you. It looks like this: 195
Printing Data Matrix 2d Barcode In Objective-C
Using Barcode creation for iPhone Control to generate, create DataMatrix image in iPhone applications.
www.OnBarcode.com
QR Code 2d Barcode Encoder In Java
Using Barcode maker for Android Control to generate, create QR Code image in Android applications.
www.OnBarcode.com
Introducing Windows 7 for Developers
UCC - 12 Reader In VB.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
www.OnBarcode.com
Recognize Barcode In VB.NET
Using Barcode Control SDK for Visual Studio .NET Control to generate, create, read, scan barcode image in Visual Studio .NET applications.
www.OnBarcode.com
void Touch_FrameReported(object sender, TouchFrameEventArgs e) { throw new NotImplementedException(); }
Printing Code39 In None
Using Barcode creation for Excel Control to generate, create Code39 image in Excel applications.
www.OnBarcode.com
PDF-417 2d Barcode Drawer In None
Using Barcode maker for Word Control to generate, create PDF-417 2d barcode image in Office Word applications.
www.OnBarcode.com
Note the second parameter, TouchFrameEventArgs this object provides much of the functionality you need to handle both single touch and multitouch. We ll be using it extensively in this chapter. TouchFrameEventArgs provides a method called GetTouchPoints, which returns a collection of touch points. Each element in the collection provides data about the touch point, such as the x and y coordinates of where the user has touched to generate that touch point. In a single-touch system, such as a Tablet PC, this collection has only one element. In systems with multiple touch points, the maximum number of elements is equal to the maximum number of touch points the system supports, but only the touch points that are in use are populated. For example, if your system supports 10 touch points, but only three fingers are touching the screen, this collection is populated by three elements. In our Tablet PC, single-touch example, we know that there will only be one touch point in this collection, so we can create an instance of the TouchPoint class from the first element in the collection, like this:
TouchPoint tp = e.GetTouchPoints(this)[0];
This TouchPoint class has a Position property, which is a System.Windows.Point that provides x and y coordinates for where the screen has been touched. Recall that in our XAML we made an ellipse called finger, so we can now move that ellipse to the point of the screen that the user is touching. Here s the code:
void Touch_FrameReported(object sender, TouchFrameEventArgs e) { TouchPoint tp = e.GetTouchPoints(this)[0]; finger.SetValue(Canvas.TopProperty, tp.Position.Y); finger.SetValue(Canvas.LeftProperty, tp.Position.X); }
Now when you run your application and drag your finger or stylus around the screen, you ll see that the ellipse follows it!
Expanding the Application for Multitouch
In the previous section, you saw that the TouchFrameEventArgs object exposed a GetTouchPoints property that returned a collection of touch points. You built a sample application for a single touch point by taking the first element in this collection. In this section, you ll see how
8 Using Windows 7 Touch with Silverlight
you can extend this to multitouch by using this collection and some classes to track multiple touch points. Create a new Silverlight application, and add a new Class file to it called finger.cs. This class will be used to track a particular finger on a multitouch screen. This class will have a number of properties: an ID that will be used to track which touch point it is for; x and y values for where the touch that corresponds to this finger took place, and a TextBlock that will be used to render the word Finger followed by the ID of the touch point. For example, if the touch point has the ID 1, the TextBlock renders Finger 1. Here s the full code for this class:
using using using using using using using using using using System; System.Net; System.Windows; System.Windows.Controls; System.Windows.Documents; System.Windows.Ink; System.Windows.Input; System.Windows.Media; System.Windows.Media.Animation; System.Windows.Shapes;
namespace SilverlightTouch2 { public class Finger { public int FingerID { get; set; } public double X { get; set; } public double Y { get; set; } public TextBlock theText; public Finger() { theText = new TextBlock(); theText.SetValue(Canvas.TopProperty, 0.0); theText.SetValue(Canvas.LeftProperty, 0.0); } } }
Now let s edit the MainPage.xaml.cs to create the application. First, add the event declaration for the Touch.FrameReported event handler shown earlier:
Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);
Introducing Windows 7 for Developers You also need to add a List<T> that gives you a dynamic list of Finger instances so that you can keep track of how many fingers are touching the screen, and to ensure your application is flexible enough for devices that support a different number of touch points. Here s the code:
List<Finger> myFingers = new List<Finger>();
Make this a class-level variable so that it s available to the event handler too. Now let s see how to use TouchFrameEventArgs to manage multiple touch points. First, you create a TouchPointCollection and initialize it to the return value of the GetTouchPoints of TouchFrameEventArgs:
TouchPointCollection t = e.GetTouchPoints(this);
Next, go through each element in this collection. Earlier we mentioned that it is a collection of TouchPoint objects, and each particular touch point has a unique ID associated with it and here you will use that. If your device supports 10 touch points and you place a finger on it, that finger always has the same ID as long as it is touching the screen. Place a second finger on the screen, and similarly it has the same ID (which is different from the ID associated with the first finger), and so on. What we ll do is go through the touch points and look at the ID of each one. If it s the ID of a touch that we have not yet seen, we ll add a new entry to the List<Finger> called myFingers; otherwise, we ll update the current entry on that list with the x and y values associated with this point. Additionally, if you re adding a new finger to the List<Finger>, you also have to initialize its internal TextBlock by giving it the text Finger with the ID as discussed earlier (placing it in the appropriate x and y coordinates and adding it to the Canvas called LayoutRoot, which contains all your controls). Here s the code to do this:
for (int lp = 0; lp < t.Count; lp++) { int nID = t[lp].TouchDevice.Id; bool bFound = false; for (int nlp2 = 0; nlp2 < myFingers.Count; nlp2++) { if (myFingers[nlp2].FingerID == nID) { myFingers[nlp2].X = t[lp].Position.X; myFingers[nlp2].Y = t[lp].Position.Y; bFound = true; myFingers[nlp2].theText.SetValue(Canvas.TopProperty, myFingers[nlp2].Y); myFingers[nlp2].theText.SetValue(Canvas.LeftProperty, myFingers[nlp2].X);
Copyright © OnBarcode.com . All rights reserved.