I have a dialog and Tab Control on it. I need draw a picture in the definite rectangle on a page of the Tab Control. So I declare an object of a class that is responsible for bitmap work for example as CDIBitmap m_bmpBackground. Bitmap file is loaded with:
BOOL CDIBitmap :: Load( CFile* pFile ) {
ASSERT( pFile );
BOOL fReturn = TRUE;
try {
delete [] (BYTE*)m_pInfo;
delete [] m_pPixels;
m_pInfo = 0;
m_pPixels = 0;
DWORD dwStart = pFile->GetPosition();
BITMAPFILEHEADER fileHeader;
pFile->Read(&fileHeader, sizeof(fileHeader));
if( fileHeader.bfType != 0x4D42 )
throw TEXT("Error:Unexpected file type, not a DIB\n");
BITMAPINFOHEADER infoHeader;
pFile->Read( &infoHeader, sizeof(infoHeader) );
if( infoHeader.biSize != sizeof(infoHeader) )
throw TEXT("Error:OS2 PM BMP Format not supported\n");
int cPaletteEntries = GetPalEntries( infoHeader );
int cColorTable = 256 * sizeof(RGBQUAD);
int cInfo = sizeof(BITMAPINFOHEADER) + cColorTable;
int cPixels = fileHeader.bfSize - fileHeader.bfOffBits;
m_pInfo = (BITMAPINFO*)new BYTE[cInfo];
memcpy( m_pInfo, &infoHeader, sizeof(BITMAPINFOHEADER) );
pFile->Read( ((BYTE*)m_pInfo) + sizeof(BITMAPINFOHEADER),
cColorTable );
m_pPixels = new BYTE[cPixels];
pFile->Seek(dwStart + fileHeader.bfOffBits, CFile::begin);
pFile->Read( m_pPixels, cPixels );
CreatePalette();
m_bIsPadded = TRUE;
#ifdef _DEBUG
} catch( TCHAR * psz ) {
TRACE( psz );
#else
} catch( TCHAR * ) {
#endif
fReturn = FALSE;
}
return fReturn;
}
Then I call Invalidate() function and get into OnEraseBkgnd where a picture is actually being drawn:
BOOL CAtmNcr::OnEraseBkgnd(CDC* pDC)
{
if(m_bmpBackground.GetPixelPtr() != 0)
{
CRect rc;
m_myWnd.GetWindowRect(rc);
ScreenToClient( &rc );
int x = 0, y = 0;
m_bmpBackground.DrawDIB(pDC, rc.left, rc.top, rc.Width(), rc.Height());
}
else
return CDialog::OnEraseBkgnd(pDC);
return true;
}
DrawDIB is defined as follows:
void CDIBitmap :: DrawDIB( CDC* pDC, int x, int y, int width, int height ) {
ASSERT( pDC );
HDC hdc = pDC->GetSafeHdc();
if( m_pInfo )
StretchDIBits( hdc,
x,
y+GetHeight(),
width,
-height,
0,
0,
GetWidth(),
GetHeight(),
GetPixelPtr(),
GetHeaderPtr(),
DIB_RGB_COLORS,
SRCCOPY );
}
The loaded picture is definitely drawn in the rectangle and you see it but untill OnEraseBkgnd is left. How come? If you apply the same code to parent dialog window then it works perfectly.