The Device Context

A handle, you'll recall, is simply a number that Windows uses for internal reference to an object. You obtain the handle from Windows and then use the handle in other functions. The device context handle is your window's passport to the GDI functions. With that device context handle you are free to paint your client area and make it as beautiful or as ugly as you like.

The device context (also called the ”DC“) is really a data structure maintained by GDI. A device context is associated with a particular display device, such as a printer, plotter, or video display. For a video display, a device context is usually associated with a particular window on the display.

Windows uses the values in the device context structure (also called ”attributes“ of the device context) in conjunction with the GDI functions. With TextOut, for instance, the attributes of the device context determine the color of the text, the color of the text background, how the x-coordinate and y-coordinate are mapped to the client area of the window, and what font Windows uses when displaying the text.

When a program needs to paint, it must first obtain a handle to a device context. After it has finished painting, the program should release the handle. When a program releases the handle, the handle is no longer valid and must not be used. The program should obtain the handle and release the handle during processing of a single message. Except for a device context created with a call to CreateDC, you should not keep a device context handle around from one message to another.

Windows applications generally use two methods for getting the handle to the device context in preparation for painting the screen.