1.2.12 Class and Private Display Contexts

A display context is a special set of values that applications use for drawing in the client area of their windows. Windows requires a display context for each window on the system display, but allows some flexibility in how that display context is stored and treated by the system.

If no explicit display-context style is given, Windows assumes that each window will use a display context retrieved from a pool of contexts maintained by Windows. In such cases, each window must retrieve and initialize the display context before painting, and then free it after painting.

In order not to retrieve a display context each time it wants to paint in a window, an application can specify the CS_OWNDC style for the window class. This class style directs Windows to create a private display context, that is, to allocate a unique display context for each window in the class. The application need only retrieve the context once, and then use it for all subsequent painting. Although the CS_OWNDC style is convenient, it must be used carefully because each display context occupies approximately 800 bytes of memory in the GDI heap.

By specifying the CS_CLASSDC style, an application can have some of the convenience of a private display context without allocating a separate display context for each window. The CS_CLASSDC style directs Windows to create a single class display context, that is, one display context to be shared by all windows in the class. An application need only retrieve the display context for a window; then as long as no other window in the class retrieves that display context, the window can continue to use the context.

Similarly, by specifying the CS_PARENTDC style, an application can create child windows that inherit the device context of their parent.