【发布时间】:2012-12-16 18:50:49
【问题描述】:
如何在 Metro 风格 C# 应用程序中获取按下指针的类型(鼠标左键或鼠标右键)?我没有在任何 Metro 风格 UI 元素中找到 MouseLeftButtonDown 事件处理程序。我应该改用PointerPressed 事件,但我不知道如何获取按下了哪个按钮。
【问题讨论】:
-
有示例代码here
标签: c# windows-8 microsoft-metro
如何在 Metro 风格 C# 应用程序中获取按下指针的类型(鼠标左键或鼠标右键)?我没有在任何 Metro 风格 UI 元素中找到 MouseLeftButtonDown 事件处理程序。我应该改用PointerPressed 事件,但我不知道如何获取按下了哪个按钮。
【问题讨论】:
标签: c# windows-8 microsoft-metro
PointerPressed 足以处理鼠标按键:
void MainPage_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// Check for input device
if (e.Pointer.PointerDeviceType == Windows.Devices.Input.PointerDeviceType.Mouse)
{
var properties = e.GetCurrentPoint(this).Properties;
if (properties.IsLeftButtonPressed)
{
// Left button pressed
}
else if (properties.IsRightButtonPressed)
{
// Right button pressed
}
}
}
【讨论】:
MousePressed 事件时,它只会在鼠标右键单击时触发。
您可以使用以下事件来确定使用的指针和按下的按钮。
private void Target_PointerMoved(object sender, PointerRoutedEventArgs e)
{
Windows.UI.Xaml.Input.Pointer ptr = e.Pointer;
Windows.UI.Input.PointerPoint ptrPt = e.GetCurrentPoint(Target);
if (ptrPt.Properties.IsLeftButtonPressed)
{
//Do stuff
}
if (ptrPt.Properties.IsRightButtonPressed)
{
//Do stuff
}
}
【讨论】:
处理 UWP 项目和以前的答案,例如 Properties.IsLeftButtonPressed/IsRightButtonPressed 对我不起作用。这些值总是错误的。我在调试期间意识到 Properties.PointerUpdateKind 正在根据鼠标按钮进行更改。这是对我有用的结果:
var properties = e.GetCurrentPoint(this).Properties;
if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.LeftButtonReleased)
{
}
else if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.RightButtonReleased)
{
}
else if (properties.PointerUpdateKind == Windows.UI.Input.PointerUpdateKind.MiddleButtonReleased)
{
}
PointerUpdateKind 中有更多选项,例如示例中的 ButtonPressed 变体和 XButton 变体,例如XButton1Pressed、XButton2Released 等。
【讨论】:
PointerReleased 事件,而不是其他答案使用的 PointerPressed 事件。