Touch Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides an application-level service that processes multitouch input from the operating system and raises the FrameReported event.

Namespace:   System.Windows.Input
Assembly:  PresentationCore (in PresentationCore.dll)


public static class Touch


Occurs when a touch message is sent.

The FrameReported event is included in Windows Presentation Foundation (WPF) to support compatibility with Silverlight. If you do not have to ensure compatibility with Silverlight, use the touch events, such as TouchDown and TouchMove, on UIElement, UIElement3D, or ContentElement.

The FrameReported event does not use the same event model as other WPF input events, such as TouchDown and TouchMove. Instead of being exposed as an element-specific event that potentially routes through the object tree of a UI, the FrameReported event is a single event that is handled at the application level. Therefore, you cannot use the sender parameter of the event handler to determine what element is touched.

Use the TouchFrameEventArgs to get the TouchPoint values that are relevant to the touch event. From the TouchPoint, you can get the Position of the touch and determine whether the TouchAction was a Down, Move, or Up action. You can also use the TouchPoint to get the TouchDevice. From the TouchDevice, you can determine the device Id and get information about the element that is touched.

The following example enables you to create simple patterns on a Canvas by dragging two fingers across a touchscreen. Each touch is represented by a TouchDevice.The pattern is created by drawing a line between the touch points 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.

<Window x:Class="WpfTouchFrameSample.MainWindow"
        Title="MainWindow" Height="525" Width="525">
        <Canvas x:Name="canvas1"
                Width="500" Height="500"
                Background="Black" />     

The following code handles the FrameReported event. When a touch presses on the Canvas, the TouchDevice is captured to the Canvas. When the touch is lifted, the TouchDevice is released. When a touch moves across 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;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;

namespace WpfTouchFrameSample
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        // Variables for tracking the position of two points.
        Point pt1, pt2 = new Point();

        public MainWindow()
            Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);

        void Touch_FrameReported(object sender, TouchFrameEventArgs e)
            if (this.canvas1 != null)
                foreach (TouchPoint _touchPoint in e.GetTouchPoints(this.canvas1))
                    if (_touchPoint.Action == TouchAction.Down)
                        // Clear the canvas and capture the touch to it.

                    else if (_touchPoint.Action == TouchAction.Move && e.GetPrimaryTouchPoint(this.canvas1) != null)
                        // This is the first (primary) touch point. Just record its position.
                        if (_touchPoint.TouchDevice.Id == e.GetPrimaryTouchPoint(this.canvas1).TouchDevice.Id)
                            pt1.X = _touchPoint.Position.X;
                            pt1.Y = _touchPoint.Position.Y;

                        // This is not the first touch point. Draw a line from the first point to this one.
                        else if (_touchPoint.TouchDevice.Id != e.GetPrimaryTouchPoint(this.canvas1).TouchDevice.Id)
                            pt2.X = _touchPoint.Position.X;
                            pt2.Y = _touchPoint.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;

                    else if (_touchPoint.Action == TouchAction.Up)
                        // If this touch is captured to the canvas, release it.
                        if (_touchPoint.TouchDevice.Captured == this.canvas1)

.NET Framework
Available since 4.0
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