BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 1.1+

Detecting Mouse Wheel Movement

The mouse wheel can often provide a handy alternative to slower mouse operations, such as scrolling areas without positioning the cursor within a scroll bar. Many Windows Forms controls have built-in wheel support but custom wheel actions are possible.

MouseWheel Event

Many software applications include support for user interaction using the mouse wheel, where one is present. Usually the mouse wheel enhances features that can be used with other mouse or keyboard operations. For example, you can scroll documents with the keyboard or by clicking and dragging areas of scroll bars. With the mouse wheel, scrolling can be easier because the pointer does not need to be positioned over the small area of a scroll bar.

If you wish to implement mouse wheel support in your own Windows Forms projects, there are two common approaches. Firstly, you can rely upon the support for mouse wheel actions that is built into many Windows Forms controls. For custom actions, such as zooming, you can detect when the mouse wheel is moved whilst the mouse pointer is over a specific control using the MouseWheel event, which is defined in the Control base class.

Sample Form

To demonstrate the use of the MouseWheel event, let's create a sample project. Create a new Windows Forms application project in Visual Studio. Once prepared, add three labels to the automatically generated form. Name the labels, DeltaLabel, TotalDeltaLabel and LinesLabel. Set the Text property of all of the labels to "0".

The image below shows a sample layout for the form. In this case three additional labels are included to show the purpose for the three that will show values.

MouseWheel event demo form

Capturing Mouse Wheel Movement

The MouseWheel event passes details of the user's activity in a MouseEventArgs object. This includes the Delta property, which returns the amount of movement of the wheel since the last event was raised. Positive values indicate forward, or upward, movement of the wheel. When the mouse wheel is rotated towards the user, the Delta value is negative. The scale of the number increases as the wheel is turned more quickly.

To demonstrate, let's capture the MouseWheel event for the entire form. To begin, register the event in the form's constructor:

public Form1()
{
    InitializeComponent();
    this.MouseWheel += Form_MouseWheel;
}

We need a field in the form's class to hold the total amount that the wheel has moved since the form was loaded. Add an integer field for this purpose:

int _totalDelta = 0;

Finally, let's add the code for the event. We'll show the immediate Delta value in the DeltaLabel, and the total movement since the program started in the TotalDelta label:

void Form_MouseWheel(object sender, MouseEventArgs e)
{
    _totalDelta = _totalDelta + e.Delta;
    DeltaLabel.Text = e.Delta.ToString();
    TotalDeltaLabel.Text = _totalDelta.ToString();
}

Mouse Wheel Scroll Lines

If you are using the MouseWheel event to scroll information, particularly when the scrolled elements include text, you should ensure that the speed of scrolling is standardised. Users are able to specify the sensitivity of the mouse wheel in the Control Panel, setting the number of lines of text that should be scrolled for a particular amount of mouse wheel rotation. The number of lines can be read from the SystemInformation class's static MouseWheelScrollLines property. This distance should be scrolled for each delta increment of 120.

To show how to determine the number of lines to scroll, modify the event code as shown below.

void Form_MouseWheel(object sender, MouseEventArgs e)
{
    _totalDelta = _totalDelta + e.Delta;
    DeltaLabel.Text = e.Delta.ToString();
    TotalDeltaLabel.Text = _totalDelta.ToString();
    LinesLabel.Text =
        (SystemInformation.MouseWheelScrollLines * _totalDelta / 120).ToString();
}

Run the program and use the mouse wheel to see the values update. Try changing the mouse wheel sensitivity in the Control Panel and running the program again, noting the difference in the displayed values.

20 February 2014