This problem has been recounted numerous times on the Internet, but I have been unable to figure out how to fix it given my particular code.
I have a dialog based app with a CStatic (picture) image container and use the following code to open a bitmap.
CString m_csPathname, m_csFilename;
const wchar_t fileDialogFilter =
const wchar_t fileDialogExt = _T("bmp");
if (fileDialog.DoModal() == IDOK)
m_csPathname = fileDialog.GetPathName();
m_csFilename = fileDialog.GetFileName();
// Load the Image File
HBITMAP hBmp = (HBITMAP)::LoadImage(NULL, m_csPathname, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
// Create a Bitmap Object and Attach It to the Object
// Create a Memory DC and Select the BMP to It
// You also need to store the old BMP pointer:
CBitmap *pOldbmp = bmDC.SelectObject(&bmp);
// Get the BMP Height and Width
// Obtain this from CBitmap's GetBitmap function.
int bufsize = bmp.GetBitmap(&bi);
_RPT1(0, "bufsize =: %d\n", bufsize);
//====== try to capture the bmp bits in a buffer
// DWORD bmp.GetBitmapBits(DWORD dwCount, LPVOID lpBits)const;
// get the bitmap height - if negative
// Pixel array (bitmap data)The pixel array is a block of m_nSize-bit DWORDs, that describes the image pixel by pixel.
// Normally pixels are stored "upside-down" with respect to normal image raster scan order, starting in the lower
// left corner, going from left to right, and then row by row from the bottom to the top of the image.
// Unless BITMAPCOREHEADER is used, uncompressed Windows bitmaps also can be stored from the top to bottom, when
// the Image Height value is negative.
DWORD dwCount = bi.bmWidth * bi.bmHeight * 4; // should be the actual required buff size
byte * lpBits = new byte[dwCount];
DWORD dw = bmp.GetBitmapBits(dwCount, lpBits);
delete  lpBits; lpBits = NULL;
This code works just fine for the given bitmap (omm1.bmp, 288 x 288 pixels), but if the bitmap is partially covered by moving the dialog partially off screen, or if the dialog is minimized, then restored, the image disappears (fails to repaint). Any suggestions as to how to remedy this would be greatly appreciated.
To set the bitmap for a static, simply send it a STM_SETIMAGE message, with the bitmap handle cast to an LPARAM.
IIRC, you are responsible for ownership of the bitmap, perhaps as a member variable of the class. I could be wrong.
In any event, the code above could not possible work across minimization etc., since you are not allowing the static control to paint itself, from the static's OnPaint handler. But by far STM_SETIMAGE is simpler.