2009-06-10 59 views
2

我有一個按鈕,只要點擊那個按鈕,我就會觸發OnClick。我想知道該按鈕上點擊了哪個鼠標按鈕?如何確定哪個鼠標按鈕在WPF中引發了單擊事件?

當我使用Mouse.LeftButtonMouse.RightButton,都告訴我「實現」這是他們的點擊後的狀態。

我只想知道哪一個點擊了我的按鈕。如果我將EventArgs更改爲MouseEventArgs,則會收到錯誤消息。

XAML:<Button Name="myButton" Click="OnClick">

private void OnClick(object sender, EventArgs e) 
{ 
//do certain thing. 
} 
+0

感謝Jose編輯我的帖子。 – paradisonoir 2009-06-10 23:11:02

+0

我找到了一個更好的方法,然後我最初建議,並編輯了我的答案,包括它。 – rmoore 2009-06-10 23:48:11

回答

2

如果您仍然需要具體瞭解它是左側還是右側按鈕,那麼您可以使用SystemInformation來獲取它。

void OnClick(object sender, RoutedEventArgs e) 
    { 
     if (SystemParameters.SwapButtons) // Or use SystemInformation.MouseButtonsSwapped 
     { 
      // It's the right button. 
     } 
     else 
     { 
      // It's the standard left button. 
     } 
    } 

編輯:的WPF相當於SystemInformation是SystemParameters,它可以用來代替。儘管您可以將System.Windows.Forms作爲參考來獲取SystemInformation,但不會以任何方式影響應用程序。

0

你說得對,何塞,它與鼠標點擊事件。但你必須添加一個小委託:

this.button1.MouseDown + = new System.Windows.Forms.MouseEventHandler(this.MyMouseDouwn);

和使用表單此方法:如果你只是使用按鈕的Click事件,那麼唯一的鼠標按鈕將火是鼠標主按鍵

private void MyMouseDouwn(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Right) 
      this.Text = "Right"; 

     if (e.Button == MouseButtons.Left) 
      this.Text = "Left"; 
    } 
+2

這對WPF來說是不正確的,你需要使用MouseButtonEventArgs,它沒有button屬性,而是每個按鈕的狀態。 – rmoore 2009-06-10 23:20:10

2

你可以施放象下面這樣:

MouseEventArgs myArgs = (MouseEventArgs) e; 

,然後得到與信息:

if (myArgs.Button == System.Windows.Forms.MouseButtons.Left) 
{ 
    // do sth 
} 

解決方案的工作在VS2013,你不必再使用鼠標點擊事件;)

相關問題