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 is the developer of Aba Search and Replace, a tool for replacing text in multiple files. He likes to program in C with a bit of C++, also in x86 assembly language, Python, and PHP.

Created by Peter Kankowski
Last changed



What does the user have to do to get WHEEL_PAGESCROLL sent?

Peter Kankowski,
Hi Adrian, it's a good question. I cannot find the answer in the documentation and I don't currently have a mouse (using a touchpad now). If you can test the program with your mouse and try various wheel movements, can you get WHEEL_PAGESCROLL sent?

Your name:


Please ignore this field: