2010-07-09 93 views
4

我正在改造windows掃雷(從XP開始),它們包含的內容是,如果您在同一時間點擊具有與左右鼠標按鈕數量一樣多的標誌的數字,則會顯示其他所有隱藏的磁磚那個數字。檢測左右兩側同時點擊鼠標?

我很難告訴我什麼時候在同一時間按下左右鼠標按鈕......我使用一對布爾,每個按鈕都有一個OnMouseDown和OnMouseUp事件,但如果在完全相同的時間(或真正關閉)單擊2個按鈕,則只有一個MouseDown事件會關閉,另一個不會...如果單擊並按住其中一個按鈕,然後單擊並按住另一個,則代碼雖然工作。

有沒有更好的方法來檢測這種「雙」點擊?

編輯:

好吧,小故事,爲什麼我搞砸這件事(它一起工作了)。

我有一臺運行Windows 7的macbook pro。對於那些不知道的人,macbook pro有一個單擊欄,通常是鼠標左鍵點擊,但是如果你放下兩個手指點擊右鍵,所以你不能這樣做(也不能中途點擊)。所以我正在構建我的應用程序並將它發送給我的朋友,他告訴我它沒有工作,所以我發佈了這個問題。我終於決定在我的其他筆記本電腦上試用它,戴爾XPS有2個鼠標按鈕......一旦它在那裏工作,我就將它傳遞給其他朋友,並且他們證實了它的工作。我不知道我的第一個朋友是如何搞砸它的,但故事的道理是不要購買任何Apple。至少這是我得到的道德。

+0

C#無法檢測_either_鼠標點擊。您需要Windows窗體,Web窗體,WPF或SilverLight。你有什麼想法? – 2010-07-09 02:48:04

回答

7

創建左一類布爾變量和右鍵默認爲false。當鼠標停止事件觸發時,將變量設置爲true並檢查兩者是否都爲真。當鼠標起火時將變量設置爲false。

public bool m_right = false; 
    public bool m_left = false; 

    private void MainForm_MouseDown(object sender, MouseEventArgs e) 
    { 
     m_objGraphics.Clear(SystemColors.Control); 

     if (e.Button == MouseButtons.Left) 
      m_left = true; 
     if (e.Button == MouseButtons.Right) 
      m_right = true; 

     if (m_left == false || m_right == false) return; 
     //do something here 
    } 

    private void MainForm_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) 
      m_left = false; 
     if (e.Button == MouseButtons.Right) 
      m_right = false; 
    } 
+0

在MouseUp中取消設置 – 2010-07-09 02:24:56

+0

我標記爲答案,因爲最終,這是我應用中的代碼,即使它是我一直以來的代碼。愚蠢的mac ... – 2010-07-09 04:13:00

0

試試這個,

Private Sub Form_Click(... , ByVal e As ystem.Windows.Forms.MouseEventArgs) 

If e.Button = MouseButtons.Right And e.Button = MouseButtons.Left Then 
MsgBox ('Right & Left code') 

End If 
+2

不行。 e.Button一次不能有兩個值。這可能是一個位掩碼或東西,但是,所以'如果e.Button和(MouseButtons.Left或MouseButtons.Right)=(MouseButtons.Left或MouseButtons.Right)'可能工作。 – cHao 2010-07-09 01:57:44

+0

我試過這兩種。他們不是旗幟,他們是獨立的實體,一次只能有一個。 – 2010-07-09 02:45:05

2

完整代碼:

private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) leftPressed = true; 
     else if (e.Button == MouseButtons.Right) rightPressed = true; 


     if (leftPressed && rightPressed) 
     { 
      MessageBox.Show("Hello"); 

      // note: 
      // the following are needed if you show a modal window on mousedown, 
      // the modal window somehow "eats" the mouseup event, 
      // hence not triggering the MouseUp event below 
      leftPressed = false; 
      rightPressed = false; 
     } 


    } 

    private void Form1_MouseUp(object sender, MouseEventArgs e) 
    { 
     if (e.Button == MouseButtons.Left) leftPressed = false; 
     else if (e.Button == MouseButtons.Right) rightPressed = false; 
    } 
0

一個老問題的種類,但我碰到這個來(也不約而同地同時做一個掃雷克隆),並認爲它失去了一些東西。如果你想有兩個按鈕點擊,但仍趕上常規單按鈕點擊,以及,你可以做到以下幾點:

private bool left_down; 
private bool right_down; 
private bool both_click; 

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     left_down = true; 
     if (right_down) 
      both_click = true; 
    } 
    if (e.Button == MouseButtons.Right) 
    { 
     right_down = true; 
     if (left_down) 
      both_click = true; 
    } 
} 
private void Form1_MouseUp(object sender, MouseEventArgs e) 
{ 
    if (e.Button == MouseButtons.Left) 
    { 
     if (!right_down) 
     { 
      if (both_click) 
       //Do both-click stuff here 
      else 
       //Do left-click stuff here 
      both_click = false; 
     } 
     left_down = false; 
    } 
    if (e.Button == MouseButtons.Right) 
    { 
     if (!left_down) 
     { 
      if (both_click) 
       //Do both-click stuff here 
      else 
       //Do right-click stuff here 
      both_click = false; 
     } 
     right_down = false; 
    } 
} 

它移動檢測到鼠標向上,而不是鼠標按下。在你釋放兩個按鈕之前,它什麼也不做。這與Windows 7版本的掃雷工具幾乎完全相同,只是原件中的右鍵單獨在鼠標下運行。 (我真的更喜歡我的版本)。有一點冗餘,雙擊事件在兩個地方被調用,這取決於你是首先釋放左鍵還是右鍵,但這應該可能是單線函數調用。獎勵:您可以從其他地方檢查both_click旗幟,以便在您的光標周圍繪製提示方塊,以顯示釋放按鈕時將顯示哪些方塊。

0

另一種選擇是使用System.Windows.Forms上的靜態MouseButtons。控制

這會告訴你的鼠標按鈕,當前按下,這樣就可以做類似如下:

((Control.MouseButtons & MouseButtons.Right) == MouseButtons.Right) && 
((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left) 

您還可以檢查出MSDN example

+0

您還可以使用Control類來檢索當前鼠標位置,這些鼠標位置在任何事件處理程序之外,也可以非常方便地使用。 Control.MousePosition – James 2016-09-21 09:45:54