Add the WM_ERASEBKGND Case

You need to add a WM_ERASEBKGND case to make sure the selected background brush is used. Add the following statements to your window function:

case WM_ERASEBKGND: /* message: erase background */

UnrealizeObject(hBrush);

hOldBrush = SelectObject(wParam, hBrush);

GetClientRect(hWnd, (LPRECT) &Rect);

PatBlt(wParam, Rect.left, Rect.top,

Rect.right-Rect.left, Rect.bottom-Rect.top,

PATCOPY);

SelectObject(wParam, hOldBrush);

return TRUE;

The hOldBrush variable is declared as a local variable. The UnrealizeObject function sets the pattern alignment if the window has moved. The SelectObject function sets the background brush and the GetClientRect function determines which part of the client area needs to be erased. The PatBlt function copies the pattern to the update rectangle. The final SelectObject function restores the previous brush.