开发者

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

Is there any way to capture the time interval between mouse wheel scroll start and stop? Actually I want to capture the interval between the scrolling start and stop when I very quickly scroll the mouse wheel.

I have already looked at MouseWheel event but it don't fulfill my requirement. In senes that it always gives a value of Delta 120 or -120 but i want to call a function depending on the speed of the mouse scroll for example when i scroll the mouse normally i want to perform function 1 and when i scrolled the mouse very quickly i want to perform the function 2. In other words is there any way开发者_Python百科 to distinguish between the mouse scroll high and normal speed.

Any advice will be appreciated.


can't you capture the mouse wheel events and see how long between them. Basically start a timer when you get a mouse wheel event and then in the next event see what the timer is at (and so how long has elapsed between the events) to determine the speed the wheel is being turned at? If the elapsedtime is smaller than a certain threshold, perform function2 and if it is faster than a certain threshold perform function 1.

You will probably have to set it to perform function 1 if the timer goes off in case they only do a single scroll.

In fact you might be able to do it this way:

start a timer (with an interval that indicates slow mouse wheeling) in the mouse wheel event, then if the timer goes off perform function 1. If the mouse wheel event happens again before the timer has gone off then reset the timer and increment a counter (to keep track of the number in wheel events since you did stuff) then start a second (longer) timer. if the counter is greater then a certain threshold perform function 2. When the second timer elapses, reset the counter. Something along those lines should give you the ability to fire function 1 when slow wheel turning and function 2 when the wheel is rapidly turned through a few 'clicks'.

this code should give a (very dirty) indication of the sort of thing I was thinking of. After playing a little I'm not really sure it's a good solution though....

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;
    }


Take a look at the Control.MouseWheel event.


As suggested by the Sam Holder i am posting here a modified verion of his advice to help other programmers facing the same problem.

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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜