Errata for MFC Black Book

Here are some known issues with the code for MFC Black Book

The CScrollView in Listing 4.4 has a problem. You'll only see this problem if you are using METHOD=0 with Windows NT. Under Windows 95 or Windows 98, you'd be using METHOD=1 and the code works OK. To see the problem, compile the code with METHOD=1 and execute the program. Grab the scroll bar with the mouse and drag it to the bottom of the scrollbar. When you let go, you'll find that the scroll position jumps to the top of the screen.

The problem isn't really with the code per se, but rather with MFC (and Windows itself). When you drag the thumb on the scroll bar, Windows sends SB_THUMBTRACK messages with a 16-bit number that indicates where the thumb is. However, MFC unpacks this 16-bit number into a 32-bit number which makes you think it is a full-range number. It isn't. When you release the thumb, you get a SB_THUMBPOSITION message with the same problem. However, MFC doesn't use SB_THUMBPOSITION, only SB_THUMBTRACK.

Luckily, this is simple to fix in a universal way. Just override OnScroll in your view class. It should look like this:

// Fix MFC's 16-bit position for thumb tracking
BOOL CScrollerView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)
{
    if (nScrollCode/256==SB_THUMBTRACK|| (nScrollCode & 0xFF)==SB_THUMBTRACK)
        {
                SCROLLINFO sinfo;
                sinfo.cbSize=sizeof(sinfo);
                sinfo.fMask=SIF_TRACKPOS;
                ::GetScrollInfo(m_hWnd,SB_VERT,&sinfo);  
                nPos=sinfo.nTrackPos;
        }
        return CScrollView::OnScroll(nScrollCode, nPos, bDoScroll);
}

The updated H file (complete)

The updated CPP file (complete)

 

Thanks to Mohamed Jeragh for pointing out the problem.