edmund1978
Programmer
I have the following OnPaint method, which displays a map and several other items in a dialog (GameMap)in MFC. It flickers horrendously, can anyone see anything wrong with it?
void GameMap::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CBitmap bmp, bmp2, bmp3, bmp4, bmp5, *poldbmp, *poldbmp2;
CDC memdc;
// Load the bitmap resource
bmp.LoadBitmap( IDB_GRASS );
bmp2.LoadBitmap( IDB_CannonTracksBlue );
bmp3.LoadBitmap( IDB_RocketTracksBlue );
bmp4.LoadBitmap( IDB_MachinegunTracksRed );
bmp5.LoadBitmap( IDB_RocketTracksRed );
// Create a compatible memory DC
memdc.CreateCompatibleDC( &dc );
COLORREF oldColor = memdc.SetBkColor(COLORREF(0xFFFFFFFF));
COLORREF oldDCColor = dc.SetBkColor(COLORREF(0xFFFFFFFF));
// set bkclr to white; we'll need it later
poldbmp = memdc.SelectObject( &bmp );
// Copy (BitBlt) bitmap from memory DC to screen DC
CDC maskdc;
maskdc.CreateCompatibleDC(NULL); // automatically creates a 1x1 monochrome bitmap,
// which is the default for a memory DC when first created
// Note: this is a memory DC since the HDC param is NULL
CBitmap ddb;
ddb.CreateCompatibleBitmap(&maskdc, 100, 100);
// creates an (empty) full-size bitmap with a color format compatible with the
// color format of the memory DC, which is monochrome
CBitmap * pOldMemBitmap = maskdc.SelectObject(&ddb);
// select the (as of yet empty but full-sized) monochrome
// bitmap into the memory device context.
// Note: the bitmap (monochrome) is compatible with the
// memory DC (also monochrome), as it needs to be
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 30; j++)
{
// Select the bitmap into the DC
maskdc.BitBlt(0, 0, 100, 100, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(10 + (i*100),10 + (j*100), 100, 100, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(10 + (i*100),10 + (j*100), 100, 100, &memdc, 0, 0, SRCAND);
}
}
//poldbmp = memdc.SelectObject( &bmp2 );
memdc.SelectObject(&poldbmp);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
poldbmp2 = memdc.SelectObject( &bmp2 );
// automatically creates a 1x1 monochrome bitmap,
// which is the default for a memory DC when first created
// Note: this is a memory DC since the HDC param is NULL
// creates an (empty) full-size bitmap with a color format compatible with the
// color format of the memory DC, which is monochrome
pOldMemBitmap = maskdc.SelectObject(&ddb);
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(600,10, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(600,10, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
// CDialog::OnPaint();
// CBitmap ddb3;
// ddb3.CreateCompatibleBitmap(&maskdc, 40, 40);
// creates an (empty) full-size bitmap with a color format compatible with the
// color format of the memory DC, which is monochrome
//CBitmap * pOldMemBitmap3 = maskdc.SelectObject(&ddb3);
pOldMemBitmap = maskdc.SelectObject(&ddb);
poldbmp2 = memdc.SelectObject( &bmp3 );
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(148,130, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(148,130, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
pOldMemBitmap = maskdc.SelectObject(&ddb);
poldbmp2 = memdc.SelectObject( &bmp4 );
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(200,200, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(200,200, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
pOldMemBitmap = maskdc.SelectObject(&ddb);
poldbmp2 = memdc.SelectObject( &bmp5 );
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(148,200, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(148,200, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
}
void GameMap::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
CBitmap bmp, bmp2, bmp3, bmp4, bmp5, *poldbmp, *poldbmp2;
CDC memdc;
// Load the bitmap resource
bmp.LoadBitmap( IDB_GRASS );
bmp2.LoadBitmap( IDB_CannonTracksBlue );
bmp3.LoadBitmap( IDB_RocketTracksBlue );
bmp4.LoadBitmap( IDB_MachinegunTracksRed );
bmp5.LoadBitmap( IDB_RocketTracksRed );
// Create a compatible memory DC
memdc.CreateCompatibleDC( &dc );
COLORREF oldColor = memdc.SetBkColor(COLORREF(0xFFFFFFFF));
COLORREF oldDCColor = dc.SetBkColor(COLORREF(0xFFFFFFFF));
// set bkclr to white; we'll need it later
poldbmp = memdc.SelectObject( &bmp );
// Copy (BitBlt) bitmap from memory DC to screen DC
CDC maskdc;
maskdc.CreateCompatibleDC(NULL); // automatically creates a 1x1 monochrome bitmap,
// which is the default for a memory DC when first created
// Note: this is a memory DC since the HDC param is NULL
CBitmap ddb;
ddb.CreateCompatibleBitmap(&maskdc, 100, 100);
// creates an (empty) full-size bitmap with a color format compatible with the
// color format of the memory DC, which is monochrome
CBitmap * pOldMemBitmap = maskdc.SelectObject(&ddb);
// select the (as of yet empty but full-sized) monochrome
// bitmap into the memory device context.
// Note: the bitmap (monochrome) is compatible with the
// memory DC (also monochrome), as it needs to be
for (int i = 0; i < 30; i++)
{
for (int j = 0; j < 30; j++)
{
// Select the bitmap into the DC
maskdc.BitBlt(0, 0, 100, 100, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(10 + (i*100),10 + (j*100), 100, 100, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(10 + (i*100),10 + (j*100), 100, 100, &memdc, 0, 0, SRCAND);
}
}
//poldbmp = memdc.SelectObject( &bmp2 );
memdc.SelectObject(&poldbmp);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
poldbmp2 = memdc.SelectObject( &bmp2 );
// automatically creates a 1x1 monochrome bitmap,
// which is the default for a memory DC when first created
// Note: this is a memory DC since the HDC param is NULL
// creates an (empty) full-size bitmap with a color format compatible with the
// color format of the memory DC, which is monochrome
pOldMemBitmap = maskdc.SelectObject(&ddb);
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(600,10, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(600,10, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
// CDialog::OnPaint();
// CBitmap ddb3;
// ddb3.CreateCompatibleBitmap(&maskdc, 40, 40);
// creates an (empty) full-size bitmap with a color format compatible with the
// color format of the memory DC, which is monochrome
//CBitmap * pOldMemBitmap3 = maskdc.SelectObject(&ddb3);
pOldMemBitmap = maskdc.SelectObject(&ddb);
poldbmp2 = memdc.SelectObject( &bmp3 );
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(148,130, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(148,130, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
pOldMemBitmap = maskdc.SelectObject(&ddb);
poldbmp2 = memdc.SelectObject( &bmp4 );
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(200,200, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(200,200, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
pOldMemBitmap = maskdc.SelectObject(&ddb);
poldbmp2 = memdc.SelectObject( &bmp5 );
maskdc.BitBlt(0, 0, 40, 40, &memdc, 0, 0, SRCCOPY);
dc.BitBlt(148,200, 40, 40, &maskdc, 0, 0, MERGEPAINT);
dc.BitBlt(148,200, 40, 40, &memdc, 0, 0, SRCAND);
memdc.SelectObject(&poldbmp2);
maskdc.SelectObject(pOldMemBitmap);
memdc.SetBkColor(oldColor);
dc.SetBkColor(oldDCColor);
}