SetBitmap displays garbage problems...
-
Hi Guys, I have a class derived from CStatic that I used to create my own custom static display. I try to use SetBitmap to insert an image into the control as a background and then I draw the text on top of that. The problem is that sometimes the button doesn't show the image that I set into it. Sometimes it shows windows controls(minimize button, maximize button, or just garbage), but this only happens sometimes. I cannot reproduce the problem consistently so maybe I am leaking resources or something, I don't know. Here is my code. Can anyone tell what is wrong? void CStatusDisplay::SetButton(CString cszFilename, UINT nIDResource) { CBitmap cbBitmap; HBITMAP hBitmap; HBITMAP hOldBitmap=NULL; if(cszFilename!="") { if(LoadBitmapFromFile(cszFilename, cbBitmap)) { hBitmap = (HBITMAP)cbBitmap.Detach(); hOldBitmap = SetBitmap(hBitmap); } else { AfxMessageBox("Error loading bitmap in display status"); } } else { if(cbBitmap.LoadBitmap(nIDResource)) { hBitmap = (HBITMAP)cbBitmap.Detach(); hOldBitmap = SetBitmap(hBitmap); } else { AfxMessageBox("Error loading bitmap in display status"); } } if(hOldBitmap!=NULL) DeleteObject(hOldBitmap); } //This gets called in OnPaint!! void CStatusDisplay::Draw() { CClientDC dc(this); CRect rect; GetClientRect(&rect); CMemDC2 memDC(&dc, &rect,TRUE); //memDC.FillSolidRect(&rect, RGB(72,82,107)); //memDC.FillSolidRect(&rect, RGB(255,0,0)); DefWindowProc(WM_PAINT, (WPARAM)memDC.m_hDC, (LPARAM)0); //add some text on top of that ;) CFont font; LOGFONT lf; memset(&lf, 0, sizeof(LOGFONT)); lf.lfHeight = m_nFontSize; if(m_bBold) lf.lfWeight = FW_BOLD; if(m_bItalics) lf.lfItalic = TRUE; strcpy(lf.lfFaceName, m_cszFontName); font.CreateFontIndirect(&lf); CFont* def_font = memDC.SelectObject(&font); memDC.SetBkMode(TRANSPARENT); memDC.SetTextColor(m_crTextColor); UINT uFormat = NULL; if(m_cszAlign == "left") uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER |DT_LEFT | DT_END_ELLIPSIS; else if(m_cszAlign == "lefttop") uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP |DT_LEFT | DT_END_ELLIPSIS; else if(m_cszAlign == "center") uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_VCENTER |DT_CENTER | DT_END_ELLIPSIS; else if(m_cszAlign == "wordbreak") uFormat = DT_NOPREFIX | DT_VCENTER |DT_CENTER | DT_WORDBREAK; memDC.DrawText(m_cszStatus, &rect, uFormat); memDC.SelectObject(def_font); BOOL bRet = font.DeleteObject(