【发布时间】:2012-11-04 07:29:38
【问题描述】:
我正在编写一个 WPF 应用程序。 我想在鼠标停止移动时触发一个事件。
这就是我尝试的方式。我创建了一个倒计时到 5 秒的计时器。每次鼠标移动时,此计时器都会“重置”。 这个思路是,鼠标停止移动的那一刻,定时器停止重置,从5倒计时到0,然后调用tick事件处理函数,显示一个消息框。
嗯,它没有按预期工作,它让我收到大量警报消息。我做错了什么?
DispatcherTimer timer;
private void Window_MouseMove(object sender, MouseEventArgs e)
{
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 5);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
MessageBox.Show("Mouse stopped moving");
}
【问题讨论】: