TouchFrameEventArgs.GetPrimaryTouchPoint(IInputElement) 方法

定义

返回相对于指定元素的主触摸设备的当前触摸点。

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

适用于