TouchDevice 類別

定義

表示觸控螢幕上手指所產生的單一觸控輸入。

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 建立簡單的模式。 每個觸控都會以 TouchDevice 中的 TouchEventArgs表示。 此圖樣是藉由繪製觸控所提供的觸控點之間的線條來建立。 此範例需要 Windows Touch 兼容畫面。

下列標記會建立使用者介面,其中包含 Canvas 以方格為中心的 ,並附加觸控事件的事件處理程式。

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

下列程式代碼會處理觸控事件。 按下 觸控 Canvas時,會 TouchDevice 擷取至 Canvas。 當觸控隨即解除時,會 TouchDevice 放開 。 當觸控在 上 Canvas移動時, Id 會檢查 。 如果移動來自第一次觸控,則會記錄其位置。 如果移動來自第二個觸控,則會從第一個觸控的位置繪製線條到第二個觸控的位置。

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 屬性來存取 TouchDeviceTouchDevice表示螢幕上的單一觸控。 如果有多個觸控存在,請使用 Id 屬性來區分它們。

注意

這個類別包含適用於所有成員之類別層級的繼承需求。 SecurityException當衍生類別沒有完全信任權限時,會擲回 。 如需安全性需求的詳細資訊,請參閱 連結需求繼承需求

建構函式

TouchDevice(Int32)

從衍生類別中的建構函式呼叫,以將 TouchDevice 類別初始化。

屬性

ActiveSource

取得報告這個裝置之輸入的 PresentationSource

Captured

取得擷取到 TouchDevice 的項目。

CaptureMode

取得 TouchDevice 的擷取原則。

DirectlyOver

取得觸控點壓到的項目。

Dispatcher

取得與這個 Dispatcher 關聯的 DispatcherObject

(繼承來源 DispatcherObject)
Id

取得作業系統所提供之 TouchDevice 的唯一識別項。

IsActive

取得值,這個值表示裝置是否在使用中。

Target

取得項目,這個項目會接收來自 TouchDevice 的輸入。

方法

Activate()

TouchDevice 加入至輸入訊息系統。

Capture(IInputElement)

使用 Element 擷取模式,擷取對指定之項目的觸控。

Capture(IInputElement, CaptureMode)

使用指定的 CaptureMode 擷取對指定之項目的觸控。

CheckAccess()

判斷呼叫的執行是否可以存取這個 DispatcherObject

(繼承來源 DispatcherObject)
Deactivate()

從輸入訊息系統中移除 TouchDevice

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetIntermediateTouchPoints(IInputElement)

在衍生類別中遭覆寫時,傳回最後兩次觸控事件期間收集到的所有觸控點。

GetTouchPoint(IInputElement)

傳回目前觸控裝置相對於指定之項目的位置。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
OnCapture(IInputElement, CaptureMode)

當擷取到對項目的觸控時呼叫。

OnManipulationEnded(Boolean)

當操作結束後呼叫。

OnManipulationStarted()

當操作開始時呼叫。

ReportDown()

報告已在項目上按下觸控。

ReportMove()

報告觸控正在移動經過項目。

ReportUp()

報告已在項目上放開觸控。

SetActiveSource(PresentationSource)

設定報告這個裝置之輸入的 PresentationSource

Synchronize()

強制 TouchDevice 同步處理使用者介面與基礎觸控點。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
VerifyAccess()

請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。

(繼承來源 DispatcherObject)

事件

Activated

發生於當 TouchDevice 加入至輸入郵件系統時。

Deactivated

會在 TouchDevice 從輸入訊息系統移除時發生。

Updated

發生於傳送觸控訊息時。

明確介面實作

IManipulator.GetPosition(IInputElement)

傳回 IManipulator 物件的位置。

IManipulator.Id

取得作業系統所提供之 TouchDevice 的唯一識別項。

IManipulator.ManipulationEnded(Boolean)

發生於操作已經結束時。

適用於