繁体   English   中英

鼠标滚轮滚动-如何捕获滚动开始和停止之间的时间间隔?

[英]Mouse Wheel Scroll - How can I capture the time interval between start and stop of scrolling?

有没有办法捕获鼠标滚轮滚动开始和停止之间的时间间隔? 实际上,当我快速滚动鼠标滚轮时,我想捕获滚动开始和停止之间的间隔。

我已经看过MouseWheel事件,但不能满足我的要求。 从某种意义上说,它总是给出Delta 120或-120的值,但是我想根据鼠标滚动的速度调用一个函数,例如,当我正常滚动鼠标时,我想执行功能1,而当我非常滚动鼠标时我想快速执行功能2。换句话说,有什么方法可以区分鼠标高滚动和正常速度。

任何建议将被认真考虑。

您无法捕获鼠标滚轮事件并查看它们之间的间隔时间。 基本上是在收到鼠标滚轮事件时启动一个计时器,然后在下一个事件中查看计时器的位置(以及事件之间经过了多长时间)来确定滚轮的旋转速度? 如果经过时间小于某个阈值,则执行功能2;如果它大于某个阈值,则执行功能1。

如果计时器关闭(如果它们仅执行一次滚动),则可能必须将其设置为执行功能1。

实际上,您可能可以这样进行:

在鼠标滚轮事件中启动一个计时器(间隔时间表示鼠标滚轮慢速),然后如果计时器关闭,则执行功能1。如果在计时器关闭之前再次发生鼠标滚轮事件,请重置计时器并增加计数器(以跟踪自您进行填充以来发生的车轮事件的数量),然后启动第二个(更长的)计时器。 如果计数器更大,则某个阈值执行功能2。当第二个计时器过去时,请重置计数器。 遵循这些原则,您应该能够在慢速转动车轮时触发功能1,而在通过几次“咔嗒”声快速转动车轮时触发功能2。

这段代码应该(非常肮脏)表明我在想什么。 玩了一点之后,我不太确定这是一个好的解决方案。

private void mouseWheelHandler (object sender, MouseEventArgs e)
    {
    slowTimer.Enabled = false;
    slowTimer.Stop ();            
    slowTimer.Interval = 200;
    slowTimer.Start();
    slowTimer.Enabled = true;
    m_counter++;
    Trace.WriteLine(string.Format("counter={0}", m_counter));
    if (fastTimer.Enabled==false)
        {
        fastTimer.Enabled = true;
        fastTimer.Interval = 150;
        fastTimer.Start ();
        }
    if (m_counter>5)
        {
        Trace.WriteLine("called method 2");
        m_counter = 0;
        fastTimer.Stop ();
        slowTimer.Enabled = false;
        slowCheckTimer.Stop ();                
        slowCheckTimer.Interval = 250;
        slowCheckTimer.Start();
        slowCheckTimer.Enabled = true;
        }
    }

private void slowTimer_Tick(object sender, EventArgs e)
    {
    Trace.WriteLine("slow timer ticked");
    if (slowCheckTimer.Enabled==false)
        {
        Trace.WriteLine ("called method 1");
        }

    slowTimer.Enabled = false;
    }

private void fastTimer_Tick(object sender, EventArgs e)
    {
    fastTimer.Enabled = false;
    Trace.WriteLine("fast timer ticked");
    m_counter = 0;
    fastTimer.Stop ();
    }

private void slowCheckTimer_Tick(object sender, EventArgs e)
    {
    Trace.WriteLine("slow check timer ticked");
    slowCheckTimer.Stop ();
    slowCheckTimer.Enabled = false;
    }

看一下Control.MouseWheel事件。

正如萨姆·霍尔德(Sam Holder)所建议的那样,我在此处发布了他的建议的修改版本,以帮助其他面临相同问题的程序员。

public partial class Form1 : Form
{
    int m_counter = 0;

    public Form1()
    {
        InitializeComponent();

        // Attach Mouse Wheel Event
        this.MouseWheel += new MouseEventHandler(Form1_MouseWheel);
    }

    void Form1_MouseWheel(object sender, MouseEventArgs e)
    {
        // Refresh Slow Timer
        slowTimer.Enabled = false;
        slowTimer.Stop();
        slowTimer.Interval = 150;
        slowTimer.Start();
        slowTimer.Enabled = true;

        // Incremenet counter
        m_counter++;

        // Start Fast Timer
        if (fastTimer.Enabled == false)
        {
            fastTimer.Enabled = true;
            fastTimer.Interval = 50;
            fastTimer.Start();
        }

        // If this returns true call
        // the fast scroll implementation
        if (m_counter > 4)
        {
            Console.WriteLine("Quick Method Called");
            m_counter = 0;
            fastTimer.Stop();
            slowTimer.Enabled = false;
            slowCheckTimer.Stop();
            slowCheckTimer.Interval = 200;
            slowCheckTimer.Start();
            slowCheckTimer.Enabled = true;
        }
    }

    private void slowTimer_Tick(object sender, EventArgs e)
    {            
        if (slowCheckTimer.Enabled == false)
        {
            Console.WriteLine("Slow Method Called");
        }

        slowTimer.Enabled = false;
    }

    private void fastTimer_Tick(object sender, EventArgs e)
    {
        fastTimer.Enabled = false;            
        m_counter = 0;
        fastTimer.Stop();
    }

    private void slowCheckTimer_Tick(object sender, EventArgs e)
    {            
        slowCheckTimer.Stop();
        slowCheckTimer.Enabled = false;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM