I’m working on implementing double buffering using a memory DC in an MFC project that utilizes CScrollView
. My custom view class is derived from CScrollView
.
However, I encounter an issue when scrolling down: the newly visible areas are not drawn.
I attempted to reset the viewport origin with memDC.SetViewportOrg(0, 0);
but this did not resolve the issue. Moreover, the previously visible areas are redrawn incorrectly after scrolling — they do not align with the amount I’ve scrolled.
Surprisingly, I found a workaround that contradicts the official MSDN description of the BitBlt
parameters.
Could someone help clarify why the observed behavior differs from the MSDN documentation? What might I be missing in handling scrolling and redrawing correctly in a CScrollView
with double buffering?
Here’s the code without all the miscellaneous bits:
void CMyScrollView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CRect rect;
GetClientRect(&rect);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height());
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
// set the viewport origin to reflect the mouse scroll.
CPoint scrollPos = GetDeviceScrollPosition();
memDC.SetViewportOrg(-scrollPos.x, -scrollPos.y);
// custom drawing function.
myDrawingFunction(&memDC);
// reset the viewport origin.
// memDC.SetViewportOrg(0, 0);
pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
// This works, but seems quite hacky and it doesn't seem to match the MSDN description of the parameters.
// pDC->BitBlt(0, 0, rect.Width()+scrollPos.x, rect.Height()+scrollPos.y, &memDC, 0, 0, SRCCOPY)
memDC.SelectObject(pOldBitmap);
}