做出延迟的mousedown事件

时间:2013-01-26 12:45:23

标签: c# winforms mousedown

我有一个datagridview,哪些单元格有一个click事件。单元格还具有以下mouseDown事件:

if (e.Button == MouseButtons.Left && e.Clicks == 1)
{
    string[] filesToDrag = 
    {
        "c:/install.log"
    };

    gridOperations.DoDragDrop(new DataObject(DataFormats.FileDrop, filesToDrag), DragDropEffects.Copy);
}

每当我尝试单击一个单元格时,mousedown事件会立即触发并尝试拖动单元格。如果用户将鼠标按下1秒钟,我怎样才能使mousedown事件触发?谢谢!

1 个答案:

答案 0 :(得分:8)

执行此操作的正确方法不是按时间进行,而是在用户移动鼠标时触发它。 Windows中“足够移动”的通用度量是双击大小。实现CellMouseDown / Move事件处理程序,类似于:

    private Point mouseDownPos;

    private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e) {
        mouseDownPos = e.Location;
    }

    private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e) {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left) {
            if (Math.Abs(e.X - mouseDownPos.X) >= SystemInformation.DoubleClickSize.Width ||
                Math.Abs(e.Y - mouseDownPos.Y) >= SystemInformation.DoubleClickSize.Height) {
                // Start dragging
                //...
            }
        }
    }