August 5th, 2003

Answer to previous exercise on proportional scrollbars

If you look at the WM_VSCROLL message, you’ll see that the scroll position is a 16-bit value. So if the number of entries is more then 65535, you won’t be able to use the scroll thumb to get to the ones at the end.

Try it: Change the value of g_cItems to 100000 and watch what happens.

The fix is to ignore the pos passed to the message and instead get it from the scrollbar. This helper function will prove useful.

UINT GetTrackPos(HWND hwnd, int fnBar)
{
    SCROLLINFO si;
    si.cbSize = sizeof(si);
    si.fMask = SIF_TRACKPOS;
    if (GetScrollInfo(hwnd, fnBar, &si)) {
        return si.nTrackPos;
    }
    return 0;
}

Change the two case statements in OnVscroll as follows:

    case SB_THUMBPOSITION:  ScrollTo(hwnd, GetScrollPos(hwnd, SB_VERT)); break;
    case SB_THUMBTRACK:     ScrollTo(hwnd, GetTrackPos(hwnd, SB_VERT)); break;
Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.