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 MustInherit Class TouchDevice
	Inherits InputDevice
	Implements IManipulator

NameDescription
System_CAPS_protmethodTouchDevice(Int32)

Called from constructors in derived classes to initialize the TouchDevice class.

NameDescription
System_CAPS_pubpropertyActiveSource

Gets the PresentationSource that is reporting input for this device.(Overrides InputDevice.ActiveSource.)

System_CAPS_pubpropertyCaptured

Gets the element that captured the TouchDevice.

System_CAPS_pubpropertyCaptureMode

Gets the capture policy of the TouchDevice.

System_CAPS_pubpropertyDirectlyOver

Gets the element that the touch contact point is directly over.

System_CAPS_pubpropertyDispatcher

Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.)

System_CAPS_pubpropertyId

Gets the unique identifier of the TouchDevice, as provided by the operating system.

System_CAPS_pubpropertyIsActive

Gets a value that indicates whether the device is active.

System_CAPS_pubpropertyTarget

Gets the element that receives input from the TouchDevice.(Overrides InputDevice.Target.)

NameDescription
System_CAPS_protmethodActivate()

Adds the TouchDevice to the input messaging system.

System_CAPS_pubmethodCapture(IInputElement)

Captures a touch to the specified element by using the Element capture mode.

System_CAPS_pubmethodCapture(IInputElement, CaptureMode)

Captures a touch to the specified element by using the specified CaptureMode.

System_CAPS_pubmethodCheckAccess()

Determines whether the calling thread has access to this DispatcherObject.(Inherited from DispatcherObject.)

System_CAPS_protmethodDeactivate()

Removes the TouchDevice from the input messaging system.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetIntermediateTouchPoints(IInputElement)

When overridden in a derived class, returns all touch points that are collected between the most recent and previous touch events.

System_CAPS_pubmethodGetTouchPoint(IInputElement)

Returns the current position of the touch device relative to the specified element.

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodOnCapture(IInputElement, CaptureMode)

Called when a touch is captured to an element.

System_CAPS_protmethodOnManipulationEnded(Boolean)

Called when a manipulation has ended.

System_CAPS_protmethodOnManipulationStarted()

Called when a manipulation is started.

System_CAPS_protmethodReportDown()

Reports that a touch is pressed on an element.

System_CAPS_protmethodReportMove()

Reports that a touch is moving across an element.

System_CAPS_protmethodReportUp()

Reports that a touch was lifted from an element.

System_CAPS_protmethodSetActiveSource(PresentationSource)

Sets the PresentationSource that is reporting input for this device.

System_CAPS_pubmethodSynchronize()

Forces the TouchDevice to synchronize the user interface with underlying touch points.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodVerifyAccess()

Enforces that the calling thread has access to this DispatcherObject.(Inherited from DispatcherObject.)

NameDescription
System_CAPS_pubeventActivated

Occurs when the TouchDevice is added to the input messaging system.

System_CAPS_pubeventDeactivated

Occurs when the TouchDevice is removed from the input messaging system.

System_CAPS_pubeventUpdated

Occurs when a touch message is sent.

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodIManipulator.GetPosition(IInputElement)

Returns the position of the IManipulator object.

System_CAPS_pubinterfaceSystem_CAPS_privmethodIManipulator.ManipulationEnded(Boolean)

Occurs when a manipulation has ended.

System_CAPS_pubinterfaceSystem_CAPS_privpropertyIManipulator.Id

Gets the unique identifier of the TouchDevice as provided by the operating system.

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.

System_CAPS_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.

Class MainWindow
    ' Variables to track the first two touch points 
    ' and the ID of the first touch point.
    Private pt1, pt2 As Point
    Private firstTouchId As Integer = -1
    ' Touch Down
    Private Sub canvas_TouchDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.TouchEventArgs)
        Dim _canvas As Canvas = CType(sender, Canvas)
        If (_canvas IsNot Nothing) Then
            _canvas.Children.Clear()
            e.TouchDevice.Capture(_canvas)

            ' Record the ID of the first touch point if it hasn't been recorded.
            If firstTouchId = -1 Then
                firstTouchId = e.TouchDevice.Id
            End If
        End If
    End Sub
    ' Touch Move
    Private Sub canvas_TouchMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.TouchEventArgs)
        Dim _canvas As Canvas = CType(sender, Canvas)
        If (_canvas IsNot Nothing) Then
            Dim tp = e.GetTouchPoint(_canvas)
            ' This is the first touch point; just record its position.
            If e.TouchDevice.Id = firstTouchId Then
                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.
            ElseIf e.TouchDevice.Id <> firstTouchId Then
                pt2.X = tp.Position.X
                pt2.Y = tp.Position.Y

                Dim _line As 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)
            End If
        End If
    End Sub
    ' Touch Up
    Private Sub canvas_TouchUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.TouchEventArgs)
        Dim _canvas As Canvas = CType(sender, Canvas)
        If (_canvas IsNot Nothing AndAlso e.TouchDevice.Captured Is _canvas) Then
            _canvas.ReleaseTouchCapture(e.TouchDevice)

        End If
    End Sub
End Class

UIPermission

for deriving from this class. Security action: InheritanceDemand. Associated enumeration: PermissionState.Unrestricted

.NET Framework
Available since 4.0
Silverlight
Available since 3.0
Windows Phone Silverlight
Available since 7.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: