且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

添加拖放后 CellDoubleClick 事件不起作用

更新时间:2023-12-03 13:33:16

是的,那行不通.调用 DoDragDrop() 将鼠标控制移交给 Windows D+D 逻辑,这将干扰正常的鼠标处理.您需要延迟启动 D+D,直到您看到用户实际拖动.这应该可以解决问题:

Yes, that cannot work. Calling DoDragDrop() turns mouse control over to the Windows D+D logic, that's going to interfere with normal mouse handling. You need to delay starting the D+D until you see the user actually dragging. This ought to solve the problem:

    Point dragStart;

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
        if (e.Button == MouseButtons.Left) dragStart = e.Location;
    }

    private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
        if (e.Button == MouseButtons.Left) {
            var min = SystemInformation.DoubleClickSize;
            if (Math.Abs(e.X - dragStart.X) >= min.Width ||
                Math.Abs(e.Y - dragStart.Y) >= min.Height) {
                // Call DoDragDrop
                //...
            }
        }
    }