UIElement.PointerMoved 事件

定义

当指针保持在此元素的命中测试区域内时,指针移动时发生。

public:
 virtual event PointerEventHandler ^ PointerMoved;
// Register
event_token PointerMoved(PointerEventHandler const& handler) const;

// Revoke with event_token
void PointerMoved(event_token const* cookie) const;

// Revoke with event_revoker
UIElement::PointerMoved_revoker PointerMoved(auto_revoke_t, PointerEventHandler const& handler) const;
public event PointerEventHandler PointerMoved;
function onPointerMoved(eventArgs) { /* Your code */ }
uIElement.addEventListener("pointermoved", onPointerMoved);
uIElement.removeEventListener("pointermoved", onPointerMoved);
- or -
uIElement.onpointermoved = onPointerMoved;
Public Custom Event PointerMoved As PointerEventHandler 
<uiElement PointerMoved="eventhandler"/>

事件类型

注解

触摸、鼠标和笔/触笔交互在 UWP 应用中作为指针输入进行接收、处理和管理。 这些交互中的任何一个都可以生成 PointerMoved 事件。 有关详细信息,请参阅本主题的 处理指针输入 和“鼠标和触笔输入的 PointerMoved”部分。

在某些 UI 方案中,尤其是当用户使用鼠标时,此事件将触发很多。 请注意放入此处理程序的代码的性能配置文件,并考虑使用自己的标志或容差的方法,这些标志或容差可能会限制逻辑实际需要运行的次数。

此事件是路由事件。 有关路由事件概念的详细信息,请参阅 事件和路由事件概述

对于触摸操作以及特定于交互的事件或一个触摸操作引起的操作事件,一个元素必须对点击测试可见,以用作事件源并触发与该操作关联的事件。 UIElement.Visibility 必须为 Visible。 派生类型的其他属性也会影响命中测试可见性。 有关详细信息,请参阅事件和路由事件概述

此事件还支持将事件处理程序附加到将调用的路由,即使事件的事件数据标记为“ 已处理”也是如此。 请参阅 AddHandler

鼠标和触笔输入的 PointerMoved

鼠标输入设备具有屏幕光标,每当鼠标超过元素的边界时,该光标都可见,即使当时未按下鼠标按钮。 类似的行为可用于笔设备输入,其中输入设备可以检测到触控笔正悬停在输入设备图面上,但未触摸它。 因此,鼠标和笔输入将比触摸输入更频繁地触发 PointerMoved 事件。 有关详细信息,请参阅鼠标交互

相比之下,仅当手指触摸表面时,才能检测到触摸点。 仅当触摸点在移动时与表面保持恒定接触时,触摸点才会生成 PointerMoved。 对于生成 PointerMoved 的此类触摸操作,操作也可能作为操作或手势进行处理。 有关详细信息,请参阅 处理指针输入

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

private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.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 == Windows.Devices.Input.PointerDeviceType.Mouse)
    {
        // To get mouse state, we need extended pointer details.
        // We get the pointer info through the getCurrentPoint method
        // of the event argument. 
        Windows.UI.Input.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);
}
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
    Windows.UI.Xaml.Input.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 == Windows.Devices.Input.PointerDeviceType.Mouse)
    {
        // To get mouse state, we need extended pointer details.
        // We get the pointer info through the getCurrentPoint method
        // of the event argument. 
        Windows.UI.Input.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);
}

某些应用程序(如游戏)需要跟踪特定功能的相对鼠标移动, (例如虚拟轨迹球或观看相机) ,并且不使用系统光标或绝对屏幕坐标。 有关隐藏鼠标光标和忽略绝对鼠标数据的详细信息,请参阅 相对鼠标移动和 CoreWindow

适用于

另请参阅