Your browser doesn't support JavaScript Device Context - Windows Programming

Device Context

In Windows, the graphics device interface (GDI) displays graphics and formatted text on both the screen and the printer. A device context is a Windows data structure containing information about the drawing attributes of the output device. Writing text and drawing images to the screen is known as ‘painting’.

System Generated Repaint Requests

Windows does not keep a record of the application window content so if by some action, a part of this client area is overwritten then widows will inform the application that this client area needs to be ‘repainted’ by posting a WM_PAINT message to the application window. The region of the application client that needs updating is known as an “invalid area”. Windows maintains the size and coordinates of this region for each Window.

Before an application can ‘repaint’ the screen it must obtain a device context for the Windows client area. Windows then fills the device context structure with the attribute values of the device being written to. In MFC, the CDC class wraps a Windows device context and the associated GDI member function for working with the ON_WM_PAINT() handler into one package CPaintDC. The constructor function for CPaintDC is

CPaintDC(CWND *window)

Where window is a pointer to the window whose client area the device context object will access. To get a DC for the invoking windows use the ‘this’ pointer

To respond to the WM_PAINT message, an MFC function will need a ON_WM_PAINT() entry in the applications message map and then write an associated OnPaint() message handler.

Other Useful Device Context-Related Functions

In addition to repainting the screen in response to an ON_WM_PAINT message, the application can also request a device context due to some action performed by the user that does not result in the generation of the On_Paint message.

CClientDC

Creates a client-area device context that is not associated with OnPaint. The constructor function for the CClientDC() is

CClientDC (CWND *window)

Where window is a pointer to the window from which the device context is obtained. To invoke a DC for the invoking windows use this as a parameter. To access the entire screen use a NULL pointer.

CWindowDC

Provides a DC to the entire window, including its client and nonclient area. The nonclient area covers the Windows borders, title bar, menu bar, minimize/maximize button, and exit button. The Non-client area is generally under the control of the operating system. The constructor function for CWindowDC() is

CWindowDC (CWND *Window)

Where window is a pointer to the window from which the device context is obtained. To gain access to the entire screen use the NULL character.

InvalidateRect

Allows an application to invalidate a Windows region manually and tells Windows it needs to repaint that region. The prototype for this function is

void InvalidateRect(LPCRECT lpRect,BOOL bErase = TRUE);

where
lpRect – is a pointer to a RECT structure that contains the update region client coordinates. If this parameter is NULL, the entire client area is set for update.
bErase – Specifies whether the background within the update region is to be erased when the update region is processed. If this parameter is TRUE, the background is erased when the BeginPaint function is called. If this parameter is FALSE, the background remains unchanged.

ValidateRect

Allows an application to validate a Windows region manually. The prototype for this function is

void ValidateRect(LPCRECT lpRect);

Where lpRect is a pointer to a RECT structure that contains the client coordinates of the rectangle to be removed from the update region. If the RECT structure is NULL the entire client area is removed from the update rectangle.

Example

The following short program demonstrates the OnPaint() message by keeping a running total of the times the client area has been repainted. The repaint request can be generated by dragging another window over the application window or by clicking the minimise and maximise icon

Download Code