2010-07-09 25 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