Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect mouse wheel direction (forwards or backwards)

I need to know how to determine the scroll whether is forward or backward(not vertical or horizontal) on MouseWheel event.

panel1.MouseWheel += ZoomIn

public void ZoomIn(object sender, EventArgs e)
{
     // Need to know whether the wheel is scrolled forwards or backwards
}
like image 882
developer_009 Avatar asked Dec 06 '22 12:12

developer_009


2 Answers

Instead of EventArgs use MouseEventArgs which expose the Delta property. It is negative on down scrolling and positive on upscrolling.

panel1.MouseWheel += ZoomIn;

public void ZoomIn(object sender, MouseEventArgs e)
{
    if(e.Delta > 0)
    {
        // The user scrolled up.
    }
    else
    {
        // The user scrolled down.
    }
}
like image 180
Thomas Flinkow Avatar answered Mar 03 '23 22:03

Thomas Flinkow


Per MSDN:

When handling the MouseWheel event it is important to follow the user interface (UI) standards associated with the mouse wheel. The MouseEventArgs.Delta property value indicates the amount the mouse wheel has been moved. The UI should scroll when the accumulated delta is plus or minus 120. The UI should scroll the number of logical lines returned by the SystemInformation.MouseWheelScrollLines property for every delta value reached. You can also scroll more smoothly in smaller that 120 unit increments, however the ratio should remain constant, that is SystemInformation.MouseWheelScrollLines lines scrolled per 120 delta units of wheel movement.

So you want to change the type of e in your handler to be MouseEventArgs (or introduce a cast), and then use its Delta property

like image 41
Rowland Shaw Avatar answered Mar 04 '23 00:03

Rowland Shaw