TouchDevice 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表由手指在觸控螢幕上產生的單點觸控輸入。
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
- 繼承
- 實作
範例
以下範例讓你只需在觸控螢幕上拖動兩指,就能在 A Canvas 上創造簡單的圖案。 每個觸碰都以 a TouchDevice 表示。TouchEventArgs 這個圖案是透過在觸碰所提供的觸點之間畫出一條線來創造的。 此範例需要 Windows 觸控相容的螢幕。
以下標記建立了使用者介面,該介面以 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房產來取得的TouchDevice。 A TouchDevice 代表螢幕上的一次點擊。 如果有多個觸碰,利用屬性 Id 來區分它們。
備註
此類別包含一個適用於所有成員的類別層級繼承需求。 當衍生類別沒有完全信任權限時,會拋出 A SecurityException 。 欲了解更多安全要求資訊,請參閱 連結要求 與 繼承要求。
建構函式
| 名稱 | Description |
|---|---|
| TouchDevice(Int32) |
從衍生類別中的建構子呼叫以初始化類別。TouchDevice |
屬性
| 名稱 | Description |
|---|---|
| ActiveSource |
會收到 PresentationSource 該裝置的輸入報告。 |
| Captured |
取得捕捉 的 TouchDevice元素。 |
| CaptureMode |
獲得 的捕獲策略 TouchDevice。 |
| DirectlyOver |
取得觸控接觸點正好在上方的元素。 |
| Dispatcher |
了解 Dispatcher 這與此 DispatcherObject 有關。 (繼承來源 DispatcherObject) |
| Id |
取得作業系統提供的唯一識別碼 TouchDevice。 |
| IsActive |
會得到一個表示裝置是否活躍的值。 |
| Target |
取得從 中接收輸入 TouchDevice的元素。 |
方法
事件
| 名稱 | Description |
|---|---|
| Activated |
當 被 TouchDevice 加入輸入訊息系統時,會發生。 |
| Deactivated |
當 從輸入訊息系統中移除時 TouchDevice ,會發生這種情況。 |
| Updated |
當發送觸控訊息時會發生。 |
明確介面實作
| 名稱 | Description |
|---|---|
| IManipulator.GetPosition(IInputElement) |
回傳物件的位置 IManipulator 。 |
| IManipulator.Id |
取得作業系統提供的唯一識別碼 TouchDevice 。 |
| IManipulator.ManipulationEnded(Boolean) |
當操控結束時會發生。 |