进行延迟的mousedown事件 [英] make delayed mousedown event
问题描述
我有一个datagridview,其中的单元格具有click事件.单元格还具有以下mouseDown事件:
I have a datagridview which cells has a click event. The cells also have the following mouseDown event:
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事件触发?谢谢!
whenever I try to click a cell, the mousedown event instantly fires and tries to drag the cell. How can I make the mousedown event fire only if user has holded mouse down for 1 second for example? Thanks!
推荐答案
执行此操作的正确方法不是按时间,而是在用户充分移动鼠标时触发它.在Windows中,移动足够"的通用度量是双击大小.实现CellMouseDown/Move事件处理程序,类似于:
The proper way to do this is not by time but to trigger it when the user moved the mouse enough. The universal measure for "moved enough" in Windows is the double-click size. Implement the CellMouseDown/Move event handlers, similar to this:
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
//...
}
}
}
这篇关于进行延迟的mousedown事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!