Getting a Device Context Handle: Method Two

You can also obtain a handle to a device context if you want to paint the client area when processing messages other then WM_PAINT or if you need the device context handle for other purposes, such as obtaining information about the device context. Call GetDC to obtain the handle to the device context, and ReleaseDC after you're done with it:

hdc = GetDC (hwnd) ;

[use GDI functions]

ReleaseDC (hwnd, hdc) ;

Like BeginPaint and EndPaint, the GetDC and ReleaseDC functions should be called in pairs. When you call GetDC while processing a message, you should call ReleaseDC before you exit the window procedure. Do not call GetDC in response to one message and ReleaseDC in response to another.

Unlike the device context handle obtained from the PAINTSTRUCT structure, the device context handle returned from GetDC has a clipping rectangle equal to the entire client area. You can paint on any part of the client area, not merely on the invalid rectangle (if indeed there is an invalid rectangle). Unlike EndPaint, ReleaseDC does not validate any invalid rectangles.