MFC – The device context classes

Windows device context is the key GDI element that refers a physical device. Device contexts are used to draw to the physical device; screen, printer, etc,. MFC provides different wrapper classes; and each class is associated with Windows device context.

Through this article, we are going to discuss different MFC’s Device Context classes.

CDC class – The base class for the device context classes

CDC class is the base class for the C++ device context classes. It has the handle to the Windows device context and some useful methods & properties to manage the device context. It also has the methods to draw on device contexts.

MFC provides different device context classes; CClientDC, CWindowDC, CPaintDC and CMetaFileDC; which are derived from CDC class. Each class’s constructor and destructor calls appropriate functions to deal with the device contexts; which makes programmer’s job bit easy.

These device context classes we use, depending on our need in the Program. We go through each of these classes, one by one.

CClientDC class

This class is used to draw in client area of the Application window. It has the device context, which maps to the client area of the window; allowed to draw within the client area of the window, not outside of it.

The class constructor, calls GetDC() method to holds the device context and the destructor calls ReleaseDC() method to release the device context.

CWindowDC class

We use this class, to draw anywhere on Application window; on menu bar or window’s title bar or on the border etc,. This class has the device context which maps to the entire area of the window.

Very often we use this class to customize the look of the non-client area of the window; like windows with rounded corners, custom-drawn title bars, etc,. Usually we use OnNcPaint() message handler, in response to WM_NCPAINT message, to paint non-client area of the Application window.

Object construction time, it calls GetWindowDC() method and once painting is done, it calls ReleaseDC() method through it’s destructor.

CPaintDC class

We should use this class in OnPaint() message handler only, in response to WM_PAINT messages, to draw on the window’s device context. The device context maps to the client area of the window.

WM_PAINT is an important message. Each Windows based application has to handle this message when Windows sends the message to the application windows to re-draw, when required.

This class constructor calls ::BeginPaint function to holds the device context; once painting is done, it release the device context through ::EndPaint function, when destructor is called .

CMetaFileDC class

Windows Meta file is an image file format; stores list of commands to be issued to draw on Windows GDI (Graphics Device Interface); whether the drawing is a vector graphics or an image, etc,.

This class implements Windows meta-file and we can use the commands stored in Meta file to replay to create the drawing.


MFC – The device context classes

Leave a Reply

Your email address will not be published.

Scroll to top