Problem with SelectObject in WM_ERASEBKGND
Hi,
I have a strange problem. I am creating a bitmap in wm_create and
using it in WM_ERASEBKGND
case WM_CREATE:
{
CDC *pDC = this->GetDC();
m_bgdc.CreateCompatibleDC( pDC );
bitmapObj.CreateCompatibleBitmap(pDC,
MAIN_WINDOW_W,
MAIN_WINDOW_H);
this->ReleaseDC(pDC);
m_oldBmp = (CBitmap *)m_bgdc.SelectObject(bitmapObj);
}
but after using it when i want to restore the old bitmap to the dc it
is crashing. pls help me to solve this issue.
case WM_ERASEBKGND:
{
CDC *pDC = CDC::FromHandle((HDC)wParam);
CRect r;
if ( m_iBkgBmpRes > ZERO )
{
CBitmap bmp;
CBrush brush;
HBITMAP bgBMP = NULL;
bgBMP = (HBITMAP)
LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(m_iBkgBmpRes),IMAGE_BITMAP,ZERO,ZERO,LR_DEFAULTCOLOR );
if (bgBMP)
{
if (bmp.Attach(bgBMP) > 0)
{
if (brush.CreatePatternBrush(&bmp) > 0)
{
GetClientRect(&r);
pDC->FillRect(r,&brush);
m_bgdc.FillRect(r,&brush);
m_bgdc.SelectObject(m_oldBmp);//Crashing here
HANDLE handl=bmp.Detach();
BOOL b1=DeleteObject(handl);
BOOL b=brush.DeleteObject();
return TRUE;
}
}
}
}
}