简体   繁体   English

检测多个鼠标单击

[英]Detect more than one mouse click

I have a problem with probably a simple solution, but I just dont get it right. 我可能有一个简单的解决方案,但我只是做错了。 So I want something to happen if I press the left mouse button. 因此,如果我按鼠标左键,我希望发生一些事情。

private void DrawingPanel_MouseClick(object sender, MouseEventArgs e)
{  
  if (e.Button == MouseButtons.Left)
  {
   //something happens
  }
}

So, that works fine. 所以,这很好。 but now the problem comes inn. 但是现在问题来了。 Now I want to check for more clicks within the click event (now that I think about it, is that even possible?) Something like this: 现在,我想检查一下click事件中是否有更多点击(现在我已经考虑过,是否有可能?)是这样的:

private void DrawingPanel_MouseClick(object sender, MouseEventArgs e)
{  
  if (e.Button == MouseButtons.Left)
  {
   //something happens
   //blabla...
   if (e.Button == MouseButtons.Left)
   {
    //Do something
   }
  }
}

Is something like that possible? 这样有可能吗? Because the second if (e.Button == MouseButtons.Left) is always true. 因为第二个if (e.Button == MouseButtons.Left)始终为true。 How can I do it so that its not automaticlly true? 我该怎么做,以至于不能自动实现?

This won't work out the way you implemented it, because within that handler you still handle the one mouse-click event. 这将无法解决您实现它的方式,因为在该处理程序中您仍然可以处理一个鼠标单击事件。

Try handling the Control.DoubleClick-event instead. 尝试改为处理Control.DoubleClick事件

The MouseClick event is re-entered for every click (not taking into account double-clicks). 每次单击都会重新输入MouseClick事件(不考虑双击)。
If you are trying to create logic for a multi-click procedure, a simple solution is maintaining a click counter on your class: 如果您试图为多次单击过程创建逻辑,则一种简单的解决方案是在类上维护点击计数器:

private int clickNo = 0;

private void DrawingPanel_MouseClick(object sender, MouseEventArgs e)
{  
   if (e.Button == MouseButtons.Left)
   {
       clickNo++;
       switch (clickNo)
       {
           case 1: Foo(); break;
           case 2: Bar(); break;
           case 3: Baz(); clickNo = 0; break;
       }
   }
}

You would actually end up with 2 click events because you have two clicks. 实际上,您将获得2次单击事件,因为您有两次单击。 If you are looking to emulate a double click functionality or something of the likes and the doubleclick event doesn't work for you, I would store the relevant data about the first click, along with a date time, out to some class scoped variables and compare to that datetime during the second click. 如果您想模拟双击功能或类似功能,但doubleclick事件对您不起作用,则将有关首次单击的相关数据以及日期时间存储到某些类范围的变量中,与第二次点击期间的日期时间进行比较。

The following emulates a double click with a limited timespan to perform the two clicks in. This case it is 300 milliseconds. 下面的模拟一个有限时间跨度的双击来执行两次单击。这种情况下为300毫秒。 The DoubleClick event also works but only passes EventArgs instead of MouseEventArgs which severely limits its use. DoubleClick事件也可以使用,但只能传递EventArgs而不是MouseEventArgs,这会严重限制其使用。

    private int _clicks = 0;
    private void DoWhateverOnDoubleClick(object sender, MouseEventArgs e)
    {
        if(e.Button != MouseButtons.Left)return; // We want left clicks only
        if (++_clicks == 2) 
            DoWhatever(); // if double click then do whatever
        Task.Run(() =>
        {
            Thread.Sleep(300);
            _clicks = 0;
        }); // Reset clicks after 300 ms

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM