BOOL CCDibDoc::Load(LPCTSTR lpszPath) { // Stop MFC assertion if GDI object already attached. if (bitmap.GetSafeHandle() != NULL) bitmap.Detach(); // Attach the HANDLE from the Windows object. return bitmap.Attach(::LoadImage(NULL, lpszPath, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE)); }5) Overide the virtual function OnOpenDocument (right click CCDibDoc in ClassView and select Add Virual Function...) and make changes to reflect the following code.
BOOL CCDibDoc::OnOpenDocument(LPCTSTR lpszPathName) { if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE; // Attempt to load a bitmap image. if (!Load(lpszPathName)) AfxMessageBox("Unable to load the bitmap file"); return TRUE; }6) In the CCDibView class make sure your OnDraw handler looks like the following code.
void CCDibView::OnDraw(CDC* pDC) { CDC memDC; BITMAP bmInfo; // Grab a pointer to the MFC document. CCDibDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // Only draw the bitmap if we have a valid GDI handle. if (pDoc->bitmap.GetSafeHandle() != NULL) { memDC.CreateCompatibleDC(pDC); CBitmap* pOld = memDC.SelectObject(&pDoc->bitmap); // Work out the bitmap dimensions. pDoc->bitmap.GetBitmap(&bmInfo); // Blit from the offscreen DC into the client area. pDC->BitBlt(1, 1, bmInfo.bmWidth, bmInfo.bmHeight, &memDC, 1, 1, SRCCOPY); pDC->TextOut(10, 10, pDoc->GetTitle()); // Clear up GDI objects. memDC.SelectObject(pOld); } }7) Build the application and run.