In this function, after about 90 calls (it's called in a loop and the idea is to load in a separate image each time, but I have kept it to one image for simplicity).The global variables now changed to local ones.
void CDLP_Printer_ControlDlg::DisplayBMPfromSVG(CString& strDsiplayFile)
{
HBITMAP hbmp_temp = (HBITMAP)::LoadImage(0, strDsiplayFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if (!hbmp_temp)
{
//hbmp_temp = ::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP1));
ActionList.AddString(L"Bitmap Load Failure: GetBMPromSVG");
ActionList.UpdateWindow();
if (!hbmp_temp)
return;
}
CBitmap bmp_temp;
bmp_temp.Attach(hbmp_temp);
mProjectorWindow.m_picControl.ModifyStyle(0xF, SS_BITMAP, SWP_NOSIZE);
mProjectorWindow.m_picControl.SetBitmap(bmp_temp);
return;
}
I hope someone can come up with an Idea whats wrong. GetLastError returns "8" which means nothing to me.
Detachwill destroy the previous handle.Note that if you call
Detachafter callingSetBitmap, the picture control's bitmap is destroyed. The result is that the picture control is painted once, but it won't be repainted. For example picture control goes blank if dialog is resized.EDIT
To destroy the old bitmap, call
Detachfollowed byDestroyObject. ExampleIf it's a temporary
CBitmapthenDeleteObjectis not necessary, becauseDeleteObjectis called automatically whenCBitmapgoes out of scope.Note that if you destroy the bitmap after calling
SetBitmap, the picture control's bitmap is destroyed. The result is that the picture control is painted once, but it won't be repainted. For example picture control goes blank if dialog is resized.It's the same problem if you declare a temporary
CBitmapon stack and attach the bitmap handle. That bitmap handle will be destroyed and picture control can't repaint itself.In addition, Windows XP sometimes makes duplicate bitmap which needs to be destroyed as well.
SetBitmapreturns a handle to previous bitmap. In Vista+ the returned bitmap is the same one which was save inm_bitmap, we already destroy that withDetach. But in XP we need to destroy this copy if it is a different handle.Also,
SetBitmaptakesHBITMAPparameter and returnsHBITMAP, so you can avoid usingCBitmapaltogether. The following example works in Vista+