To ensure that the selected background brush is used, you must create a WM_ERASEBKGND case, by adding the following statements to your window procedure:
case WM_ERASEBKGND:
UnrealizeObject(hBrush);
hOldBrush = SelectObject((HDC) wParam, (HGDIOBJ) hBrush);
GetClientRect(hwnd, &Rect);
PatBlt((HDC) wParam, Rect.left, Rect.top,
Rect.right - Rect.left, Rect.bottom - Rect.top, PATCOPY);
SelectObject((HDC) wParam, (HGDIOBJ) 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 must be erased. The PatBlt function copies the pattern to the update rectangle. The final SelectObject function restores the previous brush.