3 out of 9 rated this helpful - Rate this topic

WM_CTLCOLORSTATIC message

Applies to: desktop apps only

A static control, or an edit control that is read-only or disabled, sends the WM_CTLCOLORSTATIC message to its parent window when the control is about to be drawn. By responding to this message, the parent window can use the specified device context handle to set the text foreground and background colors of the static control.

A window receives this message through its WindowProc function.

WM_CTLCOLORSTATIC

    WPARAM wParam;
    LPARAM lParam; 

Parameters

wParam

Handle to the device context for the static control window.

lParam

Handle to the static control.

Return value

If an application processes this message, the return value is a handle to a brush that the system uses to paint the background of the static control.

Remarks

If the application returns a brush that it created (for example, by using the CreateSolidBrush or CreateBrushIndirect function), the application must free the brush. If the application returns a system brush (for example, one that was retrieved by the GetStockObject or GetSysColorBrush function), the application does not need to free the brush.

By default, the DefWindowProc function selects the default system colors for the static control.

You can set the text background color of a disabled edit control, but you cannot set the text foreground color. The system always uses COLOR_GRAYTEXT.

Edit controls that are not read-only or disabled do not send the WM_CTLCOLORSTATIC message; instead, they send the WM_CTLCOLOREDIT message.

The WM_CTLCOLORSTATIC message is never sent between threads; it is sent only within the same thread.

If a dialog box procedure handles this message, it should cast the desired return value to a INT_PTR and return the value directly. If the dialog box procedure returns FALSE, then default message handling is performed. The DWL_MSGRESULT value set by the SetWindowLong function is ignored.

Examples

The following C++ example shows how to set the text foreground and background colors of a static control in response to the WM_CTLCOLORSTATIC message. The hbrBkgnd variable is a static HBRUSH variable that is initialized to NULL, and stores the background brush between calls to WM_CTLCOLORSTATIC. The brush must be destroyed by a call to the DeleteObject function when it is no longer needed, typically when the associated dialog box is destroyed.


   case WM_CTLCOLORSTATIC:
        {
        HDC hdcStatic = (HDC) wParam;
        SetTextColor(hdcStatic, RGB(255,255,255));
        SetBkColor(hdcStatic, RGB(0,0,0));

        if (hbrBkgnd == NULL)
        {
            hbrBkgnd = CreateSolidBrush(RGB(0,0,0));
        }
        return (INT_PTR)hbrBkgnd;
        }


Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winuser.h (include Windows.h)

See also

Reference
WM_CTLCOLORBTN
WM_CTLCOLOREDIT
WM_CTLCOLORLISTBOX
WM_CTLCOLORSCROLLBAR
Other Resources
DefWindowProc
RealizePalette
SelectPalette
WM_CTLCOLORDLG

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Be prepared when you use Window themes!
This message is IMHO only sent by controls implemented in USER32.DLL, i.e. "static", "button" and similar window classes.
 
When your window is themed, either by supplying a suitable manifest file / resource, or by fusion context of a property sheet and ISOLATION_AWARE_ENABLED switch, all the well-known standard controls are sub-classified by the "themed" version of comctl32.dll.
 
As for ListView, TreeView, and some other controls introduced with Win95 and Win32s in the Mid-1990s, these controls generate WM_NOTIFY messages with NM_CUSTOMDRAW notification code instead. Therefore, when themes apply to your application, you have to handle both notifications.
 
To save writing two handlers, you can disable the theme for the specific control, but it may appear a bit alien then. See SetWindowTheme() function.
Not sent for WS_POPUP controls.
This message is not sent to the owning window if the static control is WS_POPUP rather than WS_CHILD.
HBRUSH is not deleted by the system

Creating the brush each time will cause a handle leak. It should be created once when in WM_INITDIALOG then destroyed when the dialog closes.
Another option is to use the DC brush, which is a temporary brush that exists in every DC. The brush is freed when the DC is freed.
Example in the WM_CTLCOLORSTATIC handler:

case WM_CTLCOLORSTATIC:
      // (as an aside, you should call DefWindowProc here)
          SetDCBrushColor(hdc, 0xFF00FF); // select some color into the DC brush
         return (LPARAM)GetStockBrush(DC_BRUSH); // return the DC brush -- no need to release it!
Also note that other controls that display text use this notification
Taken from http://support.microsoft.com/kb/130952:

"WM_CTLCOLORSTATIC

Sent By: Any control that displays text which would be displayed using the default dialog/window background color. This includes check boxes, radio buttons, group boxes, static text, read-only or disabled edit controls, and disabled combo boxes (all styles). 

The changes affect the text drawn in the control. Changes do not affect the checkmarks on the buttons or the outline of the group box. "
Brush resource
at least on vista, the brush is not deleted by windows. I remember the handle and delete it the next time, this funktion is executed. It works, but I am not realy shure
Value
WM_CTLCOLORSTATIC = &H138