下面使用线程的方法来屏蔽控件单击事件,这个方法更优一些。双击的时候能迅速响应,但是影响到了单击时的响应。
主要原理也是判断两个点击的差距是不是大于500ms。
要深刻理解这里的工作原理,还是需要多加强对鼠标、键盘及其它高级事件的原理。
-
private int mLastTime = 0;
-
private Thread mSClickThread;
-
public Form1()
-
{
-
InitializeComponent();
-
}
-
private void doubleClickHandler()
-
{
-
MessageBox.Show("我是双击");
-
}
-
private void simpleClickHandler()
-
{
-
MessageBox.Show("我是单击");
-
}
-
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
-
{
-
if ((Environment.TickCount - this.mLastTime) < 500)
-
{
-
Thread dClickThread = new Thread(() =>
-
{
-
doubleClickHandler();
-
}
-
);
-
dClickThread.IsBackground = true;
-
dClickThread.Priority = ThreadPriority.Highest;
-
dClickThread.Start();
-
this.mSClickThread.Abort();
-
}
-
else
-
{
-
this.mSClickThread = new Thread(new ThreadStart(() =>
-
{
-
Thread.Sleep(300);
-
simpleClickHandler();
-
})
-
);
-
this.mSClickThread.IsBackground = true;
-
this.mSClickThread.Start();
-
}
-
}
-
private void dataGridView1_MouseUp(object sender, MouseEventArgs e)
-
{
-
this.mLastTime = Environment.TickCount;
-
}
参考源工程代码:
WindowsFormsApplication13.rar
阅读(4296) | 评论(0) | 转发(0) |