3.1.1 Using the GetDC Function

Typically, an application uses the GetDC function to provide an instant response to some action by the user, such as drawing a line as the user moves the cursor (pointer) through the window. The function returns a device-context handle that the application can use in any GDI output function.

The following example shows how to use the GetDC function to retrieve a device-context handle and write the string “Hello Windows!” in the client area:

hDC = GetDC(hWnd);
TextOut(hDC, 10, 10, "Hello, Windows!", 15);
ReleaseDC(hWnd, hDC);

In this example, the GetDC function returns the device-context handle for the window identified by the hWnd parameter, and the TextOut function writes the string at the coordinates (10,10) in the window's client area. The ReleaseDC function releases the device context.

Because Windows sends a WM_ERASEBKGND message to the window procedure while processing a WM_PAINT message, anything your application draws in the client area will be erased the next time the window procedure receives a WM_PAINT message that affects that part of the client area. If the application passes WM_ERASEBKGND on to the DefWindowProc function, that function fills the affected area by using the class background brush, erasing any output previously drawn there.