For one rotation of the wheel, never scroll the window by more than one page.

Created by Peter Kankowski
Last changed
Filed under Win32 programming

Share on social sitesReddit Digg Delicious Buzz Facebook Twitter

Corrections to Raymond Chen's wheel scrolling code

In 2003, Raymond Chen published a long series of articles about scrolling in Win32. Unfortunately, the wheel scrolling technique has a subtle bug: if the window height is less than 3 lines (more precisely, less than the scroll delta, which has a default value of 3), several lines will be skipped when scrolling.

Before scrolling: He took his vorpal blade in hand / Long time before the manxsome for he sought. After scrolling: And stood awhile in thought

As you can see, one line (about the Tumturn tree :) is missing. A better program will never scroll the window by more than one page for one rotation of the wheel. Change these lines:

if(uScroll == WHEEL_PAGESCROLL) {
   uScroll = g_cLinesPerPage;

to the following ones:

if(uScroll > g_cLinesPerPage) {
   uScroll = g_cLinesPerPage;

WHEEL_PAGESCROLL is defined as UINT_MAX, so the modified code will handle WHEEL_PAGESCROLL as well as the small window height.

The code for handling sub-detent wheel mice also was simplified in the program below. Such beasts currently do not exist, so instead of writing the sophisticated code for line-by-line scrolling with them, you could scroll by 3 lines as with usual mouse. The function is simpler this way.

Download the full code (MSVC++ 2005 or Pelles C, 6 KB zip archive)

Peter Kankowski
Peter Kankowski

About the author

Peter lives in Siberia, the land of sleeping sun, beautiful mountains, and infinitely deep snow. He likes to program in C with a bit of C++, also in x86 assembly language, Python, and PHP (on Windows platform). He can be reached at kankowski@narod.ru.

Leave your comment

Your name:

Please ignore this field: