Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TouchDevice Class

Represents a single touch input produced by a finger on a touchscreen.

Namespace:  System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)
[UIPermissionAttribute(SecurityAction.InheritanceDemand, Unrestricted = true)]
public abstract class TouchDevice : InputDevice, 
	IManipulator

The TouchDevice type exposes the following members.

  NameDescription
Protected methodTouchDeviceCalled from constructors in derived classes to initialize the TouchDevice class.
Top
  NameDescription
Public propertyActiveSourceGets the PresentationSource that is reporting input for this device. (Overrides InputDevice.ActiveSource.)
Public propertyCapturedGets the element that captured the TouchDevice.
Public propertyCaptureModeGets the capture policy of the TouchDevice.
Public propertyDirectlyOverGets the element that the touch contact point is directly over.
Public propertyDispatcherGets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)
Public propertyIdGets the unique identifier of the TouchDevice, as provided by the operating system.
Public propertyIsActiveGets a value that indicates whether the device is active.
Public propertyTargetGets the element that receives input from the TouchDevice. (Overrides InputDevice.Target.)
Top
  NameDescription
Protected methodActivateAdds the TouchDevice to the input messaging system.
Public methodCapture(IInputElement)Captures a touch to the specified element by using the Element capture mode.
Public methodCapture(IInputElement, CaptureMode)Captures a touch to the specified element by using the specified CaptureMode.
Public methodCheckAccessDetermines whether the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Protected methodDeactivateRemoves the TouchDevice from the input messaging system.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetIntermediateTouchPointsWhen overridden in a derived class, returns all touch points that are collected between the most recent and previous touch events.
Public methodGetTouchPointReturns the current position of the touch device relative to the specified element.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnCaptureCalled when a touch is captured to an element.
Protected methodOnManipulationEndedCalled when a manipulation has ended.
Protected methodOnManipulationStartedCalled when a manipulation is started.
Protected methodReportDownReports that a touch is pressed on an element.
Protected methodReportMoveReports that a touch is moving across an element.
Protected methodReportUpReports that a touch was lifted from an element.
Protected methodSetActiveSourceSets the PresentationSource that is reporting input for this device.
Public methodSynchronizeForces the TouchDevice to synchronize the user interface with underlying touch points.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodVerifyAccessEnforces that the calling thread has access to this DispatcherObject. (Inherited from DispatcherObject.)
Top
  NameDescription
Public eventActivatedOccurs when the TouchDevice is added to the input messaging system.
Public eventDeactivatedOccurs when the TouchDevice is removed from the input messaging system.
Public eventUpdatedOccurs when a touch message is sent.
Top
  NameDescription
Explicit interface implemetationPrivate methodIManipulator.GetPositionReturns the position of the IManipulator object.
Explicit interface implemetationPrivate propertyIManipulator.IdGets the unique identifier of the TouchDevice as provided by the operating system.
Explicit interface implemetationPrivate methodIManipulator.ManipulationEndedOccurs when a manipulation has ended.
Top

You typically access a TouchDevice by using the TouchEventArgs.TouchDevice property. A TouchDevice represents a single touch on a screen. If multiple touches are present, use the Id property to distinguish between them.

NoteNote

This class contains an inheritance demand at the class level that applies to all members. A SecurityException is thrown when the derived class does not have full-trust permission. For more information about security demands, see Link Demands and Inheritance Demands.

The following example enables you to create simple patterns on a Canvas by dragging two fingers on a touchscreen. Each touch is represented by a TouchDevice in the TouchEventArgs. The pattern is created by drawing a line between the touch points that are provided by the touches. This example requires a Windows Touch–compatible screen.

The following markup creates the user interface, which consists of a Canvas that is centered in a grid, and attaches the event handlers for the touch events.

<Window x:Class="WpfTouchEventsSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="525" Width="525">
    <Grid>
        <Canvas x:Name="canvas1"
                Width="500" Height="500"
                Background="Black"
                TouchDown="canvas_TouchDown"
                TouchMove="canvas_TouchMove"
                TouchUp="canvas_TouchUp" />
    </Grid>
</Window>

The following code handles the touch events. When a touch is pressed on the Canvas, the TouchDevice is captured to the Canvas. When the touch is lifted, the TouchDevice is released. When a touch moves on the Canvas, the Id is checked. If the move came from the first touch, its location is recorded. If the move came from the second touch, a line is drawn from the position of the first touch to the position of the second touch.

using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;

namespace WpfTouchEventsSample
{
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window
    {
        // Variables to track the first two touch points  
        // and the ID of the first touch point. 
        private Point pt1, pt2 = new Point();
        private int firstTouchId = -1;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void canvas_TouchDown(object sender, TouchEventArgs e)
        {
            Canvas _canvas = (Canvas)sender as Canvas;
            if (_canvas != null)
            {
                _canvas.Children.Clear();
                e.TouchDevice.Capture(_canvas);

                // Record the ID of the first touch point if it hasn't been recorded. 
                if (firstTouchId == -1)
                    firstTouchId = e.TouchDevice.Id;
            }
        }

        private void canvas_TouchMove(object sender, TouchEventArgs e)
        {
            Canvas _canvas = (Canvas)sender as Canvas;
            if (_canvas != null)
            {
                TouchPoint tp = e.GetTouchPoint(_canvas);
                // This is the first touch point; just record its position. 
                if (e.TouchDevice.Id == firstTouchId)
                {
                    pt1.X = tp.Position.X;
                    pt1.Y = tp.Position.Y;
                }
                // This is not the first touch point; draw a line from the first point to this one. 
                else if (e.TouchDevice.Id != firstTouchId)
                {
                    pt2.X = tp.Position.X;
                    pt2.Y = tp.Position.Y;

                    Line _line = new Line();
                    _line.Stroke = new RadialGradientBrush(Colors.White, Colors.Black);
                    _line.X1 = pt1.X;
                    _line.X2 = pt2.X;
                    _line.Y1 = pt1.Y;
                    _line.Y2 = pt2.Y;

                    _line.StrokeThickness = 2;
                    _canvas.Children.Add(_line);
                }
            }
        }

        private void canvas_TouchUp(object sender, TouchEventArgs e)
        {
            Canvas _canvas = (Canvas)sender as Canvas;
            if (_canvas != null && e.TouchDevice.Captured == _canvas)
            {
                _canvas.ReleaseTouchCapture(e.TouchDevice);
            }
        }
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.