为什么在同一元素上的 mouseDown 事件触发后不触发双击事件? [英] Why doesn't doubleclick event fire after mouseDown event on same element fires?

查看:26
本文介绍了为什么在同一元素上的 mouseDown 事件触发后不触发双击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在控件上有一个 mousedown 事件和 click 事件.mousedown 事件用于开始拖放操作.我使用的控件是 Dirlistbox.

I have a mousedown event and click event on a control. the mousedown event is used for starting dragdrop operation. The control I am using is a Dirlistbox.

 Private Sub Dir1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Dir1.MouseDown

    Dim lab As New Label
    lab.Text = Dir1.DirList(Dir1.DirListIndex)
    lab.DoDragDrop(lab, DragDropEffects.Copy)

End Sub

但是当我点击控件时,只有 mousedown 事件触发,click 事件不会触发.如果我在 mousedown 事件中注释掉lab.DoDragDrop(lab, DragDropEffects.Copy)",则单击事件会触发.当我点击控件时,我该怎么做才能同时触发 mousedown 和 click 事件?

But when i click on the control then only the mousedown event fires, click event does not get fire. If I comment out "lab.DoDragDrop(lab, DragDropEffects.Copy)" in the mousedown event then click event gets fire. what can I do so that both mousedown and click event gets fire when i click on the control?

推荐答案

这是设计使然.MouseDown 事件捕获鼠标,Control.Capture 属性.内置的 MouseUp 事件处理程序检查鼠标是否仍然被捕获并且鼠标没有移动太远,然后触发 Click 事件.问题是调用 DoDragDrop() 会取消鼠标捕获.必须如此,因为现在使用鼠标事件来实现拖放操作.因此,您永远不会获得 Click 或 DoubleClick 事件.

This is by design. The MouseDown event captures the mouse, Control.Capture property. The built-in MouseUp event handler checks if the mouse is still captured and the mouse hasn't moved too far, then fires the Click event. Trouble is that calling DoDragDrop() will cancel mouse capture. Necessarily so since mouse events are now used to implement the drag+drop operation. So you'll never get the Click nor the DoubleClick event.

需要响应点击拖放的控件是一个可用性问题.但是,它是可以修复的,您需要做的是确保用户已将鼠标从原始鼠标向下位置移动得足够多,然后开始拖动.让你的代码看起来像这样:

Controls that both need to respond to clicks and drag+drop are a usability problem. It is fixable however, what you need to do is ensure that the user has moved the mouse enough from the original mouse down location, then start the drag. Make your code look like this:

Private MouseDownPos As Point

Private Sub Dir1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Dir1.MouseDown
    MouseDownPos = e.Location
End Sub

Private Sub Dir1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Dir1.MouseMove
    If e.Button And MouseButtons.Left = MouseButtons.Left Then
        Dim dx = e.X - MouseDownPos.X
        Dim dy = e.Y - MouseDownPos.Y
        If Math.Abs(dx) >= SystemInformation.DoubleClickSize.Width OrElse _
           Math.Abs(dy) >= SystemInformation.DoubleClickSize.Height Then
            '' Start the drag here
            ''...
        End If
    End If
End Sub

这篇关于为什么在同一元素上的 mouseDown 事件触发后不触发双击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆