PRB: BS_GROUPBOX-Style Child Window Background Painting Wrong

This article was previously published under Q79982
This article has been archived. It is offered "as is" and will no longer be updated.
3.00 3.10WINDOWSkbprg kbprb kbcode
When a BS_GROUPBOX style window is created, its background does noterase correctly.
The parent window of the BS_GROUPBOX style window has theWS_CLIPCHILDREN style, which prevents the parent window fromerasing the group box's background.
Subclass the group box window to process the WM_ERASEBKGND messageby erasing its background. Listed below is a code fragment todemonstrate this procedure.
This behavior is by design.
The WS_CLIPCHILDREN style causes a window to exclude the areasoccupied by child windows when the window paints its client area.However, a BS_GROUPBOX style window is a static control that nevererases its background. Erasing the background removes any controls orbuttons that appear within the group box.

Therefore, when another child window is dropped over a group box andsubsequently dragged away, portions of that child window remainvisible in the group box's background. This problem does not occurwhen the parent window does not have the WS_CLIPCHILDREN style.

The following code fragment should be placed in the group box'ssubclass procedure. This code erases the background of the group box.
   case WM_ERASEBKGND:    {    HBRUSH  hBrush, hOldBrush;    HPEN    hPen, hOldPen;    RECT    rect;    HDC     hDC;    hDC = GetDC(hWnd);    // Obtain a handle to the parent window's background brush.    hBrush = GetClassWord(ghWnd, GCW_HBRBACKGROUND);    hOldBrush = SelectObject(hDC, hBrush);    // Create a background-colored pen to draw the rectangle    // borders, where gWindowColor is some globally defined    // COLORREF variable used to paint the window's background    hPen = CreatePen(PS_SOLID, 1, gWindowColor);    hOldPen = SelectObject(hDC, hPen);    // Erase the group box's background.    GetClientRect(hWnd, &rect);    Rectangle(hDC, rect.left,, rect.right, rect.bottom);    // Restore the original objects before releasing the DC.    SelectObject(hDC, hOldPen);    SelectObject(hDC, hOldBrush);    // Delete the created object.    DeleteObject(hPen);    ReleaseDC(hWnd, hDC);    // Instruct Windows to paint the group box text and frame.    InvalidateRect(hWnd, NULL, FALSE);    // Insert code here to instruct the contents of the group box    // to repaint as well.    return TRUE; // Background has been erased.    }				
3.00 3.10

Article ID: 79982 - Last Review: 12/04/2015 09:11:11 - Revision: 1.1

Microsoft Windows Software Development Kit 3.1

  • kbnosurvey kbarchive kb16bitonly kbbutton kbctrl kbprb KB79982