PointerRoutedEventArgs 类

定义

包含最后一个指针事件消息返回的参数。

public ref class PointerRoutedEventArgs sealed : RoutedEventArgs
/// [Windows.Foundation.Metadata.ContractVersion(Microsoft.UI.Xaml.WinUIContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class PointerRoutedEventArgs final : RoutedEventArgs
[Windows.Foundation.Metadata.ContractVersion(typeof(Microsoft.UI.Xaml.WinUIContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class PointerRoutedEventArgs : RoutedEventArgs
Public NotInheritable Class PointerRoutedEventArgs
Inherits RoutedEventArgs
继承
Object Platform::Object IInspectable RoutedEventArgs PointerRoutedEventArgs
属性

示例

下面的代码示例演示 输入示例中的方案 2。 此代码显示了使用 PointerPressedPointerReleasedPointerEnteredPointerExitedPointerMoved 事件的直接操作的一些使用模式。

<StackPanel x:Name="Scenario2Output" ManipulationMode="All">
  <StackPanel Orientation="Horizontal" Margin="0,10,0,0">
    <Button x:Name="scenario2Reset" Content="Reset" 
      Margin="0,0,10,0" Click="Scenario2Reset"/>
  </StackPanel>
  <StackPanel Orientation="Horizontal" >
    <ToggleSwitch x:Name="tbPointerCapture" 
      Header="Pointer Capture" FontSize="20"/>
    <TextBlock x:Name="txtCaptureStatus" 
      Style="{StaticResource BasicTextStyle}"/>
  </StackPanel>
  <Border x:Name="bEnteredExited" Background="Red" 
    Height="300" Width="450" CornerRadius="20" 
    HorizontalAlignment="Left">
    <Grid>
      <TextBlock Style="{StaticResource BasicTextStyle}" Text="" 
        HorizontalAlignment="Center" VerticalAlignment="Center" 
        x:Name="bEnteredExitedTextBlock"/>
      <Ellipse VerticalAlignment="Bottom" Stroke="Silver" 
        StrokeDashArray="2,2" StrokeThickness="2" Margin="2" 
        x:Name="bEnteredExitedTimer" Width="20" Height="20" 
        RenderTransformOrigin="0.5,0.5">
        <Ellipse.RenderTransform >
          <RotateTransform Angle="0" />
        </Ellipse.RenderTransform>
      </Ellipse>
    </Grid>
  </Border>
</StackPanel>
int _pointerCount;

public Scenario2()
{
    this.InitializeComponent();
    bEnteredExited.PointerEntered += bEnteredExited_PointerEntered;
    bEnteredExited.PointerExited += bEnteredExited_PointerExited;
    bEnteredExited.PointerPressed += bEnteredExited_PointerPressed;
    bEnteredExited.PointerReleased += bEnteredExited_PointerReleased;
    bEnteredExited.PointerMoved += bEnteredExited_PointerMoved;

    // To code for multiple Pointers (that is, fingers), 
    // we track how many entered/exited.
    _pointerCount = 0;
}

private void bEnteredExited_PointerMoved(object sender, 
    PointerRoutedEventArgs e)
{
    Scenario2UpdateVisuals(sender as Border, "Moved");
}

private void bEnteredExited_PointerReleased(object sender, 
    PointerRoutedEventArgs e)
{
    ((Border)sender).ReleasePointerCapture(e.Pointer);
    txtCaptureStatus.Text = string.Empty;
}

//Can only get capture on PointerPressed (i.e. touch down, mouse click, pen press)
private void bEnteredExited_PointerPressed(object sender, 
    PointerRoutedEventArgs e)
{
    if (tbPointerCapture.IsOn)
    {
        bool _hasCapture = ((Border)sender).CapturePointer(e.Pointer);
        txtCaptureStatus.Text = "Got Capture: " + _hasCapture;
    }
}

private void bEnteredExited_PointerExited(object sender, 
    PointerRoutedEventArgs e)
{
    _pointerCount--;
    Scenario2UpdateVisuals(sender as Border, "Exited");
}

private void bEnteredExited_PointerEntered(object sender, 
    PointerRoutedEventArgs e)
{
    _pointerCount++;
    Scenario2UpdateVisuals(sender as Border, "Entered");
}

private void Scenario2UpdateVisuals(Border border, 
    String eventDescription)
{
    switch (eventDescription.ToLower())
    {
        case "exited":
            if (_pointerCount <= 0)
            {
                border.Background = new SolidColorBrush(Colors.Red);
                bEnteredExitedTextBlock.Text = eventDescription;
            }
            break;
        case "moved":
            RotateTransform rt = 
                (RotateTransform)bEnteredExitedTimer.RenderTransform;
            rt.Angle += 2;
            if (rt.Angle > 360) rt.Angle -= 360;
            break;
        default:
            border.Background = new SolidColorBrush(Colors.Green);
            bEnteredExitedTextBlock.Text = eventDescription;
            break;
    }
}

private void Scenario2Reset(object sender, RoutedEventArgs e)
{
    Scenario2Reset();
}

private void Scenario2Reset()
{
    bEnteredExited.Background = new SolidColorBrush(Colors.Green);
    bEnteredExitedTextBlock.Text = string.Empty;
}

注解

在大多数情况下,建议通过指针事件处理程序的事件参数获取指针信息。

如果事件参数未在本质上公开应用所需的指针详细信息,则可以通过 的 GetCurrentPointGetIntermediatePoints 方法访问扩展指针 PointerRoutedEventArgs数据。 使用这些方法可指定指针数据的上下文。

事件 PointerRoutedEventArgs 数据类用于以下事件:

重要

鼠标输入与第一次检测到鼠标输入时分配的单个指针相关联。 单击鼠标按钮(左键、滚轮或右键)会通过 PointerPressed 事件在指针和该按钮之间创建一个辅助关联。 仅当释放该鼠标按钮时才引发 PointerReleased 事件(在完成该事件之前,其他按钮无法与指针关联)。 由于此排他性关联,会通过 PointerMoved 事件路由其他鼠标按钮单击。 处理此事件时,可以测试鼠标按钮状态,如以下示例所示。

private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Pointer ptr = e.Pointer;

    // Multiple, simultaneous mouse button inputs are processed here.
    // Mouse input is associated with a single pointer assigned when 
    // mouse input is first detected. 
    // Clicking additional mouse buttons (left, wheel, or right) during 
    // the interaction creates secondary associations between those buttons 
    // and the pointer through the pointer pressed event. 
    // The pointer released event is fired only when the last mouse button 
    // associated with the interaction (not necessarily the initial button) 
    // is released. 
    // Because of this exclusive association, other mouse button clicks are 
    // routed through the pointer move event.          
    if (ptr.PointerDeviceType == PointerDeviceType.Mouse)
    {
        // To get mouse state, we need extended pointer details.
        // We get the pointer info through the getCurrentPoint method
        // of the event argument. 
        PointerPoint ptrPt = e.GetCurrentPoint(Target);
        if (ptrPt.Properties.IsLeftButtonPressed)
        {
            eventLog.Text += "\nLeft button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsMiddleButtonPressed)
        {
            eventLog.Text += "\nWheel button: " + ptrPt.PointerId;
        }
        if (ptrPt.Properties.IsRightButtonPressed)
        {
            eventLog.Text += "\nRight button: " + ptrPt.PointerId;
        }
    }

    // Prevent most handlers along the event route from handling the same event again.
    e.Handled = true;

    // Display pointer details.
    updateInfoPop(e);
}

特定事件通常具有各种指针设备和指针点类中提供的信息,这些类主要仅与该事件相关。 例如,处理 PointerWheelChanged 时,您可能对 PointerPointProperties 中的 MouseWheelDelta 感兴趣。

GetCurrentPointGetIntermediatePoints 方法检索的对象通过 Properties 属性提供对扩展指针信息的访问权限,该属性获取 PointerPointProperties 对象。

在以下示例中,我们通过 PointerPoint 和 PointerPointProperties 对象获取扩展指针属性。 (请参阅 处理完整的示例的指针输入 。)

String queryPointer(PointerPoint ptrPt)
{
    String details = "";

    switch (ptrPt.PointerDeviceType)
    {
        case PointerDeviceType.Mouse:
            details += "\nPointer type: mouse";
            break;
        case PointerDeviceType.Pen:
            details += "\nPointer type: pen";
            if (ptrPt.IsInContact)
            {
                details += "\nPressure: " + ptrPt.Properties.Pressure;
                details += "\nrotation: " + ptrPt.Properties.Orientation;
                details += "\nTilt X: " + ptrPt.Properties.XTilt;
                details += "\nTilt Y: " + ptrPt.Properties.YTilt;
                details += "\nBarrel button pressed: " + ptrPt.Properties.IsBarrelButtonPressed;
            }
            break;
        case PointerDeviceType.Touch:
            details += "\nPointer type: touch";
            details += "\nrotation: " + ptrPt.Properties.Orientation;
            details += "\nTilt X: " + ptrPt.Properties.XTilt;
            details += "\nTilt Y: " + ptrPt.Properties.YTilt;
            break;
        default:
            details += "\nPointer type: n/a";
            break;
    }

    GeneralTransform gt = Target.TransformToVisual(panel);
    Point screenPoint;

    screenPoint = gt.TransformPoint(new Point(ptrPt.Position.X, ptrPt.Position.Y));
    details += "\nPointer Id: " + ptrPt.PointerId.ToString() +
        "\nPointer location (parent): " + ptrPt.Position.X + ", " + ptrPt.Position.Y +
        "\nPointer location (screen): " + screenPoint.X + ", " + screenPoint.Y;
    return details;
}

通常,此方法返回的对象用于将指针数据馈送给 GestureRecognizer。 另一种情况是获取 PointerWheelChanged 事件的 MouseWheelDelta;该值位于 PointerPointProperties 中。

属性

Handled

获取或设置一个值,该值将路由事件标记为已处理,并阻止事件路由中的大多数处理程序再次处理同一事件。

IsGenerated

获取一个值,该值指示指针事件是由用户与对象直接交互发生的,还是由平台基于对应用程序的 UI 的更改生成的。

KeyModifiers

获取一个值,该值指示哪些键修饰符在启动指针事件时处于活动状态。

OriginalSource

获取对引发 事件的 对象的引用。 这通常是控件的模板部分,而不是在应用 UI 中声明的元素。

(继承自 RoutedEventArgs)
Pointer

获取对指针标记的引用。

方法

GetCurrentPoint(UIElement)

检索 PointerPoint 对象,该对象提供有关与事件关联的指针的基本信息。

GetIntermediatePoints(UIElement)

检索 PointerPoint 对象的集合,这些对象表示从最后一个指针事件到并包括当前指针事件的指针历史记录。 集合中的每个 PointerPoint 都提供有关与事件关联的指针的基本信息。 集合中的最后一项等效于 GetCurrentPoint 返回的 PointerPoint 对象。

适用于

另请参阅