LoadBitmap(), how to avoid GDI Object buildup
VS 2005, MFC Dialog-based app. I am drawing a diagram and, depending on a
condition, add one of three bitmaps to the diagram. How do I properly
release the bitmaps when I exit the dialog? Here is the code I am using, and
the bitmaps are released, but the code in OnDestroy() looks strange to me.
// load the bitmaps
BOOL CMyDialog::On InitDialog()
{
....
bmp1.LoadBitap("Bitmap1");
bmp2.LoadBitmap("Bitmap2");
bmp3.LoadBitmap("Bitmap3");
....
}
void CMyDialog::OnPaint()
{
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
CBitmap Bmp;
Bmp.CreateCompatibleBitmap(&dc,size_a,size_b);
CBitmap *pOldBmp = MemDC.SelectObject(&Bmp);
DrawDiagram(&memDC);
// add one of three bitmaps based on condition
CDC tempDC;
tempDC.CreateCompatibleDC(&tempDC);
if (m_condition==1)
tempDC.SelectObject(&bmp1);
if(m_condition==2)
tempDC.SelectObject(&bmp2);
else
tempDC.SelectObject(&bmp3);
MemDC.BitBlt(position_c,position_d,24,20,&tempDC,0,0,SRCCOPY);
// now draw the combined graphics to screen
dc.BitBlt(0,0,
aa,cc,&MemDC,0,0,SRCCOPY);
MemDC.SelectObject(pOldBmp);
}
// clean up
void CMyDialog::OnDestroy()
{
DeleteObject(bvmp1);
DeleteObject(bvmp2);
DeleteObject(bvmp3);
bmp1.Detach();
bmp2.Detach();
bmp2.Detach();
}
My question is: How do I properly clean up the GDI objects bmp1, bmp2, bmp3?
The way I do it in OnDestroy() works, but it looks strange to me. Thanks for
your comments.
Harald