Holy cow, I wrote a book!
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.
pos
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:
OnVscroll
case SB_THUMBPOSITION: ScrollTo(hwnd, GetScrollPos(hwnd, SB_VERT)); break; case SB_THUMBTRACK: ScrollTo(hwnd, GetTrackPos(hwnd, SB_VERT)); break;