2012-12-16 85 views
7

如何在Metro風格的C#應用​​程序中獲取按下指針的類型(鼠標左鍵向下或鼠標右鍵向下)?我沒有在任何Metro風格的UI元素中找到MouseLeftButtonDown事件處理程序。我應該使用PointerPressed事件,但我不知道如何獲得按下哪個按鈕。PointerPressed:左鍵還是右鍵?

+2

有示例代碼[這裏](http://msdn.microsoft.com/en-us/library/windows/apps/windows .ui.xaml.uielement.pointerpressed) –

回答

8

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 
     } 
    } 
} 
3

您可以使用以下事件來確定使用了哪個指針以及按下了哪個按鈕。

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 
    } 
}