Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 7: GDI Accessories and Tools


CPaintDC: Unlike the CDC object, the CPaintDC inherently initializes its drawing by
calling the BeginPaint() function when you declare a CPaintDC variable. When the
drawing with this class is over, it calls the EndPaint() to terminate the drawing.

CClientDC: This class is used when you want to draw in the client area of a window.

CMetaFileDC: This class is used to create Windows metafiles.

6.2 The Process of Drawing..............................................................................


6.2.1 Getting a Device Context...................................................................


In order to draw using a device context, you must first declare a variable of the CDC
class. This can be done as follows:

CDC dc;

To help with this, the CView class provides a virtual member function that can carry the
drawing assignments on a program. The method provided is OnDraw() and its syntax is:

void OnDraw(CDC* pDC) = 0;

This means that each class that derives from CView must provides its own
implementation of this method. If you use AppWizard to create an application and select
a CView-derived base class, the AppWizard would define a basic implementation of
OnDraw() for you. For a CView-based application, this can be a good place to perform a
lot of your drawing.

As you can see, the OnDraw() method is passed a pointer to CDC. This allows you to use
the pDC pointer as a variable and draw on the view object with it.

Declaring a CDC variable or receiving it as an argument to a function gives you a device
context (DC) you can use. This DC initializes the drawing with some default objects such
as a pen to draw the most basic points or lines.

6.2.2 Starting a Device Context's Shape....................................................


To keep track of the various drawings, the device context uses a coordinate system that
has its origin (0, 0) on the top-left corner of the desktop:
Free download pdf