如何在 WPF 中正确记录鼠标滚轮事件的长度



该程序的目标是记录鼠标滚轮旋转的速度,时间和幅度。我可以在鼠标滚轮事件中记录振幅。但我的困惑是我可以记录多长时间/多快?

我现在正在做的方法是在窗口中实现鼠标滚轮事件。然后在另一个调度计时器的处理程序中,我检查鼠标滚轮事件是否持续一段时间(例如 1 秒),我记录时间。这是正确的方法吗?

   private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
    {
        scrollVal += (Math.Abs(e.Delta) / limitScrollSpeed);
        scroll_stopwatch.Reset();     //this makes small scrolls to be one bigger scroll
        scroll_stopwatch.Start();
        scrollRecord_stopwatch.Start();     
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        if (scroll_stopwatch.ElapsedMilliseconds >= clearScrollTimeMS)  
        {               
            scroll_stopwatch.Stop();
            scroll_stopwatch.Reset();
            scrollVal = 0;
            scrollRecord_stopwatch.Stop();
            scrollDuration = (scrollRecord_stopwatch.ElapsedMilliseconds - 1000) / 1000.0f;
            scrollRecord_stopwatch.Reset();                
        }
    }

使用 MouseWheelEventArgs.Timestamp 属性。

事件处理程序如下所示:

  1. 记录第一个车轮事件的时间戳
  2. 保持运行车轮三角洲的总和
  3. 滚动停止时,将滚轮增量总和除以最后一个时间戳减去第一个时间戳。

只需使用秒表即可确定滚动是否已"停止"。

最新更新