TouchDevice TouchDevice TouchDevice TouchDevice Class

定義

表示手指在觸控螢幕上產生的單一觸控輸入。Represents a single touch input produced by a finger on a touchscreen.

public ref class TouchDevice abstract : System::Windows::Input::InputDevice, System::Windows::Input::IManipulator
public abstract class TouchDevice : System.Windows.Input.InputDevice, System.Windows.Input.IManipulator
type TouchDevice = class
    inherit InputDevice
    interface IManipulator
Public MustInherit Class TouchDevice
Inherits InputDevice
Implements IManipulator
繼承
實作

範例

下列範例可讓您在觸控式螢幕上拖曳兩Canvas個手指, 以在上建立簡單的模式。The following example enables you to create simple patterns on a Canvas by dragging two fingers on a touchscreen. 每個觸控都會TouchDevice TouchEventArgs以中的來表示。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. 這個範例需要 Windows Touch 相容畫面。This example requires a Windows Touch-compatible screen.

下列標記會建立使用者介面, 其包含Canvas在方格中置中的, 並且會附加觸控事件的事件處理常式。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. 當按下Canvas的觸控時TouchDevice , 會將捕獲到CanvasWhen a touch is pressed on the Canvas, the TouchDevice is captured to the Canvas. 當觸碰時, TouchDevice就會釋放。When the touch is lifted, the TouchDevice is released. 當觸控在上Canvas移動時Id , 會核取。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);
            }
        }
    }
}
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

備註

您通常會TouchEventArgs.TouchDevice使用TouchDevice屬性來存取。You typically access a TouchDevice by using the TouchEventArgs.TouchDevice property. TouchDevice代表螢幕上的單一觸控。A TouchDevice represents a single touch on a screen. 如果有多個觸控, 請使用Id屬性來區別它們。If multiple touches are present, use the Id property to distinguish between them.

注意

這個類別包含適用于所有成員之類別層級的繼承要求。This class contains an inheritance demand at the class level that applies to all members. 當衍生類別沒有完全信任許可權時,就會擲回。SecurityExceptionA 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.

建構函式

TouchDevice(Int32) TouchDevice(Int32) TouchDevice(Int32) TouchDevice(Int32)

從衍生類別中的建構函式呼叫,以將 TouchDevice 類別初始化。Called from constructors in derived classes to initialize the TouchDevice class.

屬性

ActiveSource ActiveSource ActiveSource ActiveSource

取得報告這個裝置之輸入的 PresentationSourceGets the PresentationSource that is reporting input for this device.

Captured Captured Captured Captured

取得擷取到 TouchDevice 的項目。Gets the element that captured the TouchDevice.

CaptureMode CaptureMode CaptureMode CaptureMode

取得 TouchDevice 的擷取原則。Gets the capture policy of the TouchDevice.

DirectlyOver DirectlyOver DirectlyOver DirectlyOver

取得觸控點壓到的項目。Gets the element that the touch contact point is directly over.

Dispatcher Dispatcher Dispatcher Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObjectGets the Dispatcher this DispatcherObject is associated with.

(Inherited from DispatcherObject)
Id Id Id Id

取得作業系統所提供之 TouchDevice 的唯一識別項。Gets the unique identifier of the TouchDevice, as provided by the operating system.

IsActive IsActive IsActive IsActive

取得值,這個值表示裝置是否在使用中。Gets a value that indicates whether the device is active.

Target Target Target Target

取得項目,這個項目會接收來自 TouchDevice 的輸入。Gets the element that receives input from the TouchDevice.

方法

Activate() Activate() Activate() Activate()

TouchDevice 加入至輸入訊息系統。Adds the TouchDevice to the input messaging system.

Capture(IInputElement) Capture(IInputElement) Capture(IInputElement) Capture(IInputElement)

使用 Element 擷取模式,擷取對指定之項目的觸控。Captures a touch to the specified element by using the Element capture mode.

Capture(IInputElement, CaptureMode) Capture(IInputElement, CaptureMode) Capture(IInputElement, CaptureMode) Capture(IInputElement, CaptureMode)

使用指定的 CaptureMode 擷取對指定之項目的觸控。Captures a touch to the specified element by using the specified CaptureMode.

CheckAccess() CheckAccess() CheckAccess() CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObjectDetermines whether the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)
Deactivate() Deactivate() Deactivate() Deactivate()

從輸入訊息系統中移除 TouchDeviceRemoves the TouchDevice from the input messaging system.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetIntermediateTouchPoints(IInputElement) GetIntermediateTouchPoints(IInputElement) GetIntermediateTouchPoints(IInputElement) GetIntermediateTouchPoints(IInputElement)

在衍生類別中遭覆寫時,傳回最後兩次觸控事件期間收集到的所有觸控點。When overridden in a derived class, returns all touch points that are collected between the most recent and previous touch events.

GetTouchPoint(IInputElement) GetTouchPoint(IInputElement) GetTouchPoint(IInputElement) GetTouchPoint(IInputElement)

傳回目前觸控裝置相對於指定之項目的位置。Returns the current position of the touch device relative to the specified element.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
OnCapture(IInputElement, CaptureMode) OnCapture(IInputElement, CaptureMode) OnCapture(IInputElement, CaptureMode) OnCapture(IInputElement, CaptureMode)

當擷取到對項目的觸控時呼叫。Called when a touch is captured to an element.

OnManipulationEnded(Boolean) OnManipulationEnded(Boolean) OnManipulationEnded(Boolean) OnManipulationEnded(Boolean)

當操作結束後呼叫。Called when a manipulation has ended.

OnManipulationStarted() OnManipulationStarted() OnManipulationStarted() OnManipulationStarted()

當操作開始時呼叫。Called when a manipulation is started.

ReportDown() ReportDown() ReportDown() ReportDown()

報告已在項目上按下觸控。Reports that a touch is pressed on an element.

ReportMove() ReportMove() ReportMove() ReportMove()

報告觸控正在移動經過項目。Reports that a touch is moving across an element.

ReportUp() ReportUp() ReportUp() ReportUp()

報告已在項目上放開觸控。Reports that a touch was lifted from an element.

SetActiveSource(PresentationSource) SetActiveSource(PresentationSource) SetActiveSource(PresentationSource) SetActiveSource(PresentationSource)

設定報告這個裝置之輸入的 PresentationSourceSets the PresentationSource that is reporting input for this device.

Synchronize() Synchronize() Synchronize() Synchronize()

強制 TouchDevice 同步處理使用者介面與基礎觸控點。Forces the TouchDevice to synchronize the user interface with underlying touch points.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)
VerifyAccess() VerifyAccess() VerifyAccess() VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。Enforces that the calling thread has access to this DispatcherObject.

(Inherited from DispatcherObject)

事件

Activated Activated Activated Activated

發生於當 TouchDevice 加入至輸入郵件系統時。Occurs when the TouchDevice is added to the input messaging system.

Deactivated Deactivated Deactivated Deactivated

會在 TouchDevice 從輸入訊息系統移除時發生。Occurs when the TouchDevice is removed from the input messaging system.

Updated Updated Updated Updated

發生於傳送觸控訊息時。Occurs when a touch message is sent.

明確介面實作

IManipulator.GetPosition(IInputElement) IManipulator.GetPosition(IInputElement) IManipulator.GetPosition(IInputElement) IManipulator.GetPosition(IInputElement)

傳回 IManipulator 物件的位置。Returns the position of the IManipulator object.

IManipulator.Id IManipulator.Id IManipulator.Id IManipulator.Id

取得作業系統所提供之 TouchDevice 的唯一識別項。Gets the unique identifier of the TouchDevice as provided by the operating system.

IManipulator.ManipulationEnded(Boolean) IManipulator.ManipulationEnded(Boolean) IManipulator.ManipulationEnded(Boolean) IManipulator.ManipulationEnded(Boolean)

發生於操作已經結束時。Occurs when a manipulation has ended.

安全性

UIPermission
衍生自這個類別。for deriving from this class. 安全性動作: InheritanceDemandSecurity action: InheritanceDemand. 相關聯的列舉:UnrestrictedAssociated enumeration: Unrestricted

適用於