I'm using the GDI to draw to a memory bitmap, which is then copied to the window using BitBlt(). When I redraw the whole window it works correctly, but when I try to update only part of the window it doesn't redraw correctly and, after weeks of trying, I still can't work out why.

Below I've included the relevant code along with the values for one debug run.

When an area of the window needs to be updated I call this function to invalidate the updated region:

Code:
void RedrawActiveArea()
{
	m_bRedrawActiveArea = true;
	InvalidateRect(&m_rctActiveAreaRect, false);
}
The values of m_rctActiveAreaRect are {top=0 bottom=373 left=586 right=605}

Invalidating a region obviously causes OnPaint to be called:

Code:
void OnPaint() 
{
	RECT rctUpdateRect;
	if (GetUpdateRect(&rctUpdateRect))
	{
		PAINTSTRUCT Ps;
		CDC* pDC = BeginPaint(&Ps);

		if (m_bFullRedraw)
		{
			m_pMainArea->FullRedraw(pDC);
			m_bFullRedraw = false;
		}
		else if (bRedrawActiveArea)
		{
			m_pMainArea->DrawActiveArea(pDC);
			m_bRedrawActiveArea = false;
		}
		else
		{
			m_pMainArea->CopyToWindow(pDC, m_rctUpdateRect);
		}
		EndPaint(&Ps);
	}
}
Here rctUpdateRect is {top=0 bottom=373 left=586 right=605} as expected. bRedrawActiveArea is true so DrawActiveArea() is called.

For debugging purposes DrawActiveArea() is just drawing a green rectangle over the invalidated rectangle:

Code:
void CMainArea::DrawActiveArea(CDC* pdcWindowDC) 
{
	m_pdcMemDC->SelectObject(penGreenPen);
	m_pdcMemDC->SelectObject(brshGreenBrush);
	m_pdcMemDC->Rectangle(&m_rctActiveAreaRect);

	int iReturn = pdcWindowDC->BitBlt(m_rctActiveAreaRect.left, 
					  m_rctActiveAreaRect.top, 
					  m_rctActiveAreaRect.right-m_rctActiveAreaRect.left,
					  m_rctActiveAreaRect.bottom-m_rctActiveAreaRect.top,
					  m_pdcMemDC,
					  m_rctActiveAreaRect.left,
					  m_rctActiveAreaRect.top, 
					  SRCCOPY);
}
Here m_rctActiveAreaRect is the same as before: {top=0 bottom=373 left=586 right=605}.

A green rectangle should be drawn over the active area in the memory bitmap, and that area should then be copied to the window, so the green rectangle should be displayed on the window. Sadly, it doesn't happen and nothing changes in the window.

If, after this process, I drag another window over my window (causing the invalidated regions to be re-copied from the memory bitmap) the green rectangle appears. It therefore seems that the rectangle is being drawn to the memory bitmap, but is failing when being copied to the window. I therefore assumed that BitBlt() was failing, but it returns 1 and nonzero indicates success.

I'm therefore completely at a loss as to why the green rectangle isn't appearing on the window first time. All the numbers are as I'd expect them to be when I debug and everything looks right to me.

Can anyone suggest what might be going wrong?

Thanks for any advice you can offer.