Knowledge blog - Home

The CScrollView class in MFC does not work for documents longer than short int (16 bits)

OnVScroll and OnHScroll methods passes the scrollbar value in the nPos parameter of the type UINT. Although scrollbars allows to set the minimum and maximum positions in the range of int (32 bits), the value of the nPos parameter is not correct if the position was outside the range of short int (16 bits).

Worse, this affects the behavior of CScrollView in such a way that the window is not repainted during dragging the scroll box outside the limits of short int.

Here is a work-around for this problem - I've overrided the OnScroll method. GetScrollInfo returns the correct value, which is then passed back to the CScrollView::OnScroll method. Tested in Microsoft eMbedded Visual C++ 4.0.

BOOL
myclass::OnScroll( UINT nScrollCode, UINT nPos, BOOL bDoScroll) 
{
	if (LOBYTE(nScrollCode) == SB_THUMBTRACK)
	{
		SCROLLINFO info;
		info.cbSize = sizeof( SCROLLINFO);
		info.fMask = SIF_TRACKPOS;
		GetScrollInfo( SB_HORZ, &info);
		nPos = info.nTrackPos;
	}
	else if (HIBYTE(nScrollCode) == SB_THUMBTRACK)
	{
		SCROLLINFO info;
		info.cbSize = sizeof( SCROLLINFO);
		info.fMask = SIF_TRACKPOS;
		::GetScrollInfo( m_hWnd, SB_VERT, &info);
		nPos = info.nTrackPos;
	}

	return CScrollView::OnScroll( nScrollCode, nPos, bDoScroll);
}