Add an
event handler for the
WM_DESTROY
windows message, then add the lines below inside it:
if (pDC != NULL) delete pDC;
if (Bitmap != NULL) delete Bitmap;
Be aware that if you select your
bitmap onto the
device context, i.e.
pDC->SelectObject(Bitmap)
, you should save the pointer to the
CBitmap
returned by the method call and restore it onto the
device context before deleting it, as shown below:
- Add the following members to your class:
CDC *pDC;
CBitmap *pBitmap;
CBitmap *pOldBitmap;
- Add the following lines to the
OnInitDialog
method of your class:
pOldBitmap = NULL;
pDC = new CDC;
if (pDC != NULL && pDC->CreateCompatibleDC(GetDC()))
{
pBitmap = new CBitmap;
if (pBitmap != NULL && pBitmap->CreateCompatibleBitmap(pDC, 640, 480))
{
pOldBitmap = pDC->SelectObject(pBitmap);
}
}
- Add the following lines to the
OnDestroy
method of your class:
if (pOldBitmap != NULL) pDC->SelectObject(pOldBitmap);
if (pDC != NULL) delete pDC;
if (pBitmap != NULL) delete pBitmap;