TouchFrameEventArgs.GetPrimaryTouchPoint(IInputElement) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳主觸控裝置相對於指定元件的當前接觸點。
public:
System::Windows::Input::TouchPoint ^ GetPrimaryTouchPoint(System::Windows::IInputElement ^ relativeTo);
public System.Windows.Input.TouchPoint GetPrimaryTouchPoint(System.Windows.IInputElement relativeTo);
member this.GetPrimaryTouchPoint : System.Windows.IInputElement -> System.Windows.Input.TouchPoint
Public Function GetPrimaryTouchPoint (relativeTo As IInputElement) As TouchPoint
參數
- relativeTo
- IInputElement
定義座標空間的元素。 若要使用WPF絕對座標,請將 relativeTo 指定為 null。
傳回
主節點 TouchDevice 相對於指定元件的當前位置;或 null 是主節點 TouchDevice 未啟用時。
範例
以下程式碼處理從 TouchFrameEventArgs中擷取的觸點。 當在 上按下 Canvas時,會 TouchDevice 被捕捉到 Canvas。 當觸感被抬起時,就會釋放。TouchDevice 當觸控移動到 Canvas時 Id ,會被檢查。 若 與 Id 主要接觸點的 相 Id 符,表示首次觸碰,則會記錄其位置。 若移動來自第二次觸球,則會從第一次觸球的位置畫一條線到第二次觸球的位置。
這個範例是類別概述中另一個較大範例 Touch 的一部分。
foreach (TouchPoint _touchPoint in e.GetTouchPoints(this.canvas1))
{
if (_touchPoint.Action == TouchAction.Down)
{
// Clear the canvas and capture the touch to it.
this.canvas1.Children.Clear();
_touchPoint.TouchDevice.Capture(this.canvas1);
}
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;
this.canvas1.Children.Add(_line);
}
}
else if (_touchPoint.Action == TouchAction.Up)
{
// If this touch is captured to the canvas, release it.
if (_touchPoint.TouchDevice.Captured == this.canvas1)
{
this.canvas1.ReleaseTouchCapture(_touchPoint.TouchDevice);
}
}
}
For Each _touchPoint In e.GetTouchPoints(Me.canvas1)
If _touchPoint.Action = TouchAction.Down Then
' Clear the canvas and capture the touch to it.
canvas1.Children.Clear()
_touchPoint.TouchDevice.Capture(canvas1)
ElseIf _touchPoint.Action = TouchAction.Move Then
' This is the first (primary) touch point. Just record its position.
If _touchPoint.TouchDevice.Id = e.GetPrimaryTouchPoint(Me.canvas1).TouchDevice.Id Then
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.
ElseIf _touchPoint.TouchDevice.Id <> e.GetPrimaryTouchPoint(Me.canvas1).TouchDevice.Id Then
pt2.X = _touchPoint.Position.X
pt2.Y = _touchPoint.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
Me.canvas1.Children.Add(_line)
End If
ElseIf _touchPoint.Action = TouchAction.Up Then
' If this touch is captured to the canvas, release it.
If (_touchPoint.TouchDevice.Captured Is canvas1) Then
canvas1.ReleaseTouchCapture(_touchPoint.TouchDevice)
End If
End If
Next
備註
在一組主動式觸控裝置中,第一個成為 Activated 觸控裝置的裝置是主要的觸控裝置。 例如,如果兩根手指同時觸碰螢幕,第一根手指朝下時,主要觸控裝置會代表。 如果在第二根手指仍放下時,第一根手指被抬起,主要觸控裝置就會變成 null。