Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Text in a DIB
Message
From
19/12/2000 09:59:45
 
 
To
19/12/2000 08:06:50
General information
Forum:
Visual C++
Category:
Coding, syntax & commands
Title:
Miscellaneous
Thread ID:
00454517
Message ID:
00455022
Views:
49
Hennie,

Here is an MFC example that allows you to open bitmap files and draw text over the top of them.

1) Create a new MFC AppWizard (exe) from File->New->Projects and call the project CDib.

2) Make sure that you select single document (Step 1) for the Doc/View model.

3) Add a new member variable to the CCDibDoc class (right click CCDibDoc in ClassView and select Add Member Variable...) called bitmap.

4) Add a new member function to the CCDibDoc class (right click CCDibDoc in ClassView and select Add Member Function...) called Load and make sure it looks like the following code.
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.

I have tested this using Visual C++ 6 SP4 on Windows 2000 if you need the whole project then I can mail it to you.

HTH
Neil
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform