c# – 如何检测MouseWheel事件在WPF中结束
发布时间:2020-12-15 18:15:43 所属栏目:百科 来源:网络整理
导读:当我滚动鼠标滚轮时,会触发几个MouseWheel事件.我正在使用这些事件来扩展一些图像. 我想在一系列MouseWheel事件结束时调用一个方法.我怎么知道他们什么时候结束? 这是我到目前为止的实施 private void ModelWindowBorder_MouseWheel(object sender,MouseWhe
|
当我滚动鼠标滚轮时,会触发几个MouseWheel事件.我正在使用这些事件来扩展一些图像.
我想在一系列MouseWheel事件结束时调用一个方法.我怎么知道他们什么时候结束? 这是我到目前为止的实施 private void ModelWindowBorder_MouseWheel(object sender,MouseWheelEventArgs e)
{
intervaltimer = null;
// Do stuff like zooming and etc
CheckEventInterval()
}
private void CheckEventInterval()
{
intervaltimer = new Stopwatch();
intervaltimer .Start();
if (intervaltimer.ElapsedMilliseconds > 50)
{
// Do some other stuff
}
}
解决方法
实际上,由于摩擦轮旋转是无止境的,因此没有特殊事件来通知使用的结束滚动.但是在您的情况下,您可以测试用户是否在短时间内停止滚动.这可以通过一个简单的计时器来完成:
//Use dispatcher timer to avoid problems when manipulating UI related objects
DispatcherTimer timer;
float someValue = 0;
public MainWindow()
{
InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += timer_Tick;
timer.Interval = TimeSpan.FromMilliseconds(500 /*Adjust the interval*/);
MouseWheel += MainWindow_MouseWheel;
}
void timer_Tick(object sender,EventArgs e)
{
//Prevent timer from looping
(sender as DispatcherTimer).Stop();
//Perform some action
Console.WriteLine("Scrolling stopped (" + someValue + ")");
//Reset for futher scrolling
someValue = 0;
}
void MainWindow_MouseWheel(object sender,MouseWheelEventArgs e)
{
//Accumulate some value
someValue += e.Delta;
timer.Stop();
timer.Start();
}
如您所见,MouseWheel事件将启动计时器.如果在计时器触发时发生新的MouseWheel事件,它将重新启动计时器.这样,只有在特定间隔内没有车轮事件时,定时器才会触发. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
