THE MFC DRAWING MECHANISM

The MFC encapsulates the Windows interface to your screen and printer and relieves you of the need to worry about much of the details involved in programming graphical output. As you saw in the last chapter, an Application Wizard-generated program already contains a class derived from CView that’s specifically designed to display document data on-screen.

The View Class in Your Application

The primary purpose of the Application Wizard-generated CSketcherView class is to display information from a document object in the client area of a document window. The class definition includes overrides for several virtual functions, but the one of particular interest here is the OnDraw() function. This is called whenever the client area of the document window needs to be redrawn. It’s the function that’s called by the application framework when a WM_PAINT message is received by your program.

The OnDraw() Member Function

The implementation of the OnDraw() function that’s created by the MFC Application Wizard looks like this:

void CSketcherView::OnDraw(CDC* /*pDC*/)
{
   CSketcherDoc* pDoc = GetDocument();
   ASSERT_VALID(pDoc);
   if(!pDoc)
     return;
        
   // TODO: add draw code for native data here
}

A pointer to a CDC class object is passed to the OnDraw() member of the view class when it is called. This object has member functions that call the Windows API functions that enable you to draw in a device context. The parameter name is commented out: you must uncomment it or substitute ...

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.