Answer to previous exercise on proportional scrollbars

Raymond Chen

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;

0 comments

Discussion is closed.

Feedback usabilla icon