c# – 如何检测MouseWheel事件在WPF中结束

前端之家收集整理的这篇文章主要介绍了c# – 如何检测MouseWheel事件在WPF中结束前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我滚动鼠标滚轮时,会触发几个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事件,它将重新启动计时器.这样,只有在特定间隔内没有车轮事件时,定时器才会触发.

原文链接:https://www.f2er.com/csharp/97972.html

猜你在找的C#相关文章