flicker-free window drawing

Started by
1 comment, last by remo 23 years, 8 months ago
    
	CPaintDC pdc(this);
	CDC dc;
	CBitmap bmp;

	bmp.CreateCompatibleBitmap (&pdc, 300, 400);

	dc.CreateCompatibleDC (&pdc);
	dc.SelectObject (&bmp);

	dc.FillRect (CRect(200, 0, 300, 401), &CBrush(RGB(128, 128, 128)));
	dc.FillRect (CRect(0, 0, 200, 401), &CBrush(RGB(0, 0, 0)));
	dc.FillRect (CRect(210, 10, 283, 130), &CBrush(RGB(0, 0, 0)));

	dc.SetTextColor (RGB(255, 255, 255));
	dc.SetBkMode (TRANSPARENT);
	dc.DrawText ("Next", CRect(210, 10, 283, 40), DT_CENTER | DT_VCENTER | DT_SINGLELINE);

	g_pGame->m_pAllBlocks->Draw (&dc, 0, 0);
	g_pGame->m_pFallingBlock->Draw (&dc, 0, 0);
	g_pGame->m_pNextBlock->Draw (&dc, 228, 40);

	pdc.BitBlt (0, 0, 300, 400, &dc, 0, 0, SRCCOPY);

	bmp.DeleteObject ();
	dc.DeleteDC ();
    
this is what i have at the moment in an OnPaint message handler, and it prevents any flickering since it uses a CBitmap as a buffer to draw onto then copies it to the window but this uses up loads of GDI and other system resources, for some reason... does anyone know of a good way to prevent window flicker? (IMPORTANT: i''m not talking about preventing the window from erasing itself when it is repainted, i mean just getting the whole window contents onto the screen at the SAME TIME)
[email=ehremo@hotmail.com][/email]
Advertisement
I think you're problem might be this:

The call dc.SelectObject(&bmp) should be:

CBitmap* pOldBmp = dc.SelectObject(&bmp);

Then previous to the call bmp.DeleteObject() you should have the following line:

dc.SelectObject(pOldBmp);

Otherwise windows looses the pointer it has to bitmaps it has allocated itself, thus using more resources than required (I think). But, you when using MFC's painting system, whenever you use SelectObject, you must store a pointer to the old resource, and then when you're done painting, reselect the old resource! Anyway, I use the same method to handle flicker-free graphics, so the mehtod you're using should be okay...


Edited by - andresen on August 2, 2000 8:50:24 AM

Edited by - andresen on August 2, 2000 8:53:21 AM
thanks, that works!
[email=ehremo@hotmail.com][/email]

This topic is closed to new replies.

Advertisement