[英]Winform MouseDoubleClick event not firing after add MouseDown event
我正在创建一个用户控件。 可以拖动,也可以选择。 并且需要可以单击和双击。
首先,我在Mouse Click 事件中添加 Selected 属性。 但它只有在Mouse up之后才会被触发。 然后我更改我的解决方案,将其添加到鼠标按下事件中:
private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
if (!Selected)
{
Selected = true;
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
}
else if(e.Button == System.Windows.Forms.MouseButtons.Right)
{
if (null != MouseRightClick)
MouseRightClick(model, this);
}
}
}
但在那之后,我无法再获得鼠标双击事件:
private void MyControl_MouseDoubleClick(object sender, MouseEventArgs e)
{
// do something
}
更新我看到你们给我的链接,我知道原因是:
调用 DoDragDrop() 将取消鼠标捕获
但是我还是想知道,有没有什么办法可以保持Drag
with Double Click
事件?
实际上,在您知道我在问题更新中所说的原因之后,修复起来非常容易。 我们应该为识别绘制声明一个变量:
private bool IsCanDraw = false;
然后你应该将它添加到 Mouse Down
private void MyControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Clicks != 1)
{
IsCanDraw = false;
}
else
{
IsCanDraw = true;
}
// your other code
}
您需要将鼠标移动更改为:
private void MyControl_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left & IsCanDraw)
{
this.DoDragDrop(this, DragDropEffects.Move);
// Disables IsCanDraw method until next drag operation
IsCanDraw = false;
}
}
如您所见,您只能在IsCanDraw == true
执行DoDragDrop()
然后鼠标双击事件就可以了。
来自(VB.NET)的原始想法:
http://vbcity.com/forums/t/100458.aspx
http://sagistech.blogspot.com/2010/03/dodragdrop-prevent-doubleclick-event.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.