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
- 繼承
- 實作
下列範例可讓您藉由在觸摸屏上拖曳兩指,在 上 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 屬性來存取 TouchDevice 。 TouchDevice表示螢幕上的單一觸控。 如果有多個觸控存在,請使用 Id 屬性來區別它們。
備註
此類別包含套用至所有成員之類別層級的繼承需求。 SecurityException當衍生類別沒有完全信任權限時,會擲回 。 如需安全性需求的詳細資訊,請參閱 連結需求 和 繼承需求。
Touch |
從衍生類別中的建構函式呼叫,以將 TouchDevice 類別初始化。 |
Active |
取得報告這個裝置之輸入的 PresentationSource。 |
Captured |
取得擷取到 TouchDevice 的項目。 |
Capture |
取得 TouchDevice 的擷取原則。 |
Directly |
取得觸控點壓到的項目。 |
Dispatcher |
取得與這個 Dispatcher 關聯的 DispatcherObject。 (繼承來源 DispatcherObject) |
Id |
取得作業系統所提供之 TouchDevice 的唯一識別項。 |
Is |
取得值,這個值表示裝置是否在使用中。 |
Target |
取得項目,這個項目會接收來自 TouchDevice 的輸入。 |
Activate() |
將 TouchDevice 加入至輸入訊息系統。 |
Capture(IInput |
使用 Element 擷取模式,擷取對指定之項目的觸控。 |
Capture(IInput |
使用指定的 CaptureMode 擷取對指定之項目的觸控。 |
Check |
判斷呼叫的執行是否可以存取這個 DispatcherObject。 (繼承來源 DispatcherObject) |
Deactivate() |
從輸入訊息系統中移除 TouchDevice。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
Get |
做為預設雜湊函式。 (繼承來源 Object) |
Get |
在衍生類別中遭覆寫時,傳回最後兩次觸控事件期間收集到的所有觸控點。 |
Get |
傳回目前觸控裝置相對於指定之項目的位置。 |
Get |
取得目前執行個體的 Type。 (繼承來源 Object) |
Memberwise |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
On |
當擷取到對項目的觸控時呼叫。 |
On |
當操作結束後呼叫。 |
On |
當操作開始時呼叫。 |
Report |
報告已在項目上按下觸控。 |
Report |
報告觸控正在移動經過項目。 |
Report |
報告已在項目上放開觸控。 |
Set |
設定報告這個裝置之輸入的 PresentationSource。 |
Synchronize() |
強制 TouchDevice 同步處理使用者介面與基礎觸控點。 |
To |
傳回代表目前物件的字串。 (繼承來源 Object) |
Verify |
請強制執行可以存取這個 DispatcherObject 的呼叫執行緒。 (繼承來源 DispatcherObject) |
Activated |
發生於當 TouchDevice 加入至輸入郵件系統時。 |
Deactivated |
會在 TouchDevice 從輸入訊息系統移除時發生。 |
Updated |
發生於傳送觸控訊息時。 |
IManipulator. |
傳回 IManipulator 物件的位置。 |
IManipulator. |
取得作業系統所提供之 TouchDevice 的唯一識別項。 |
IManipulator. |
發生於操作已經結束時。 |
產品 | 版本 |
---|---|
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
Windows Desktop | 3.0, 3.1, 5, 6, 7, 8, 9 |