WM_CTLCOLOR
hdcChild = (HDC) wParam; /* child-window display context */
hwndChild = (HWND) LOWORD(lParam); /* handle of child window */
nCtlType = (int) HIWORD(lParam); /* type of control */
The WM_CTLCOLOR message is sent to the parent of a system-defined control class or a message box when the control or message box is about to be drawn. The following controls send this message:
Combo boxes
Edit controls
List boxes
Buttons
Static controls
Scroll bars
hdcChild
Value of wParam. Identifies the display context for the child window.
hwndChild
Value of the low-order word of lParam. Identifies the child window.
nCtlType
Value of the high-order word of lParam. Specifies the type of the control. This parameter can be one of the following values:
Value | Meaning |
CTLCOLOR_BTN | Button |
CTLCOLOR_DLG | Dialog box |
CTLCOLOR_EDIT | Edit control |
CTLCOLOR_LISTBOX | List box |
CTLCOLOR_MSGBOX | Message box |
CTLCOLOR_SCROLLBAR | Scroll bar |
CTLCOLOR_STATIC | Static control |
If an application processes the WM_CTLCOLOR message, it must return a handle to the brush that is to be used for painting the control background or it must return NULL.
To change the text color, the application should call the SetTextColor function with the desired red, green, and blue (RGB) values.
To change the background color of a single-line edit control, the application must set the brush handle in both the CTLCOLOR_EDIT and CTLCOLOR_MSGBOX message codes, and the application must call the SetBkColor function in response to the CTLCOLOR_EDIT code.
The return value from this message has no effect on a button with the BS_PUSHBUTTON or BS_DEFPUSHBUTTON style.
This example creates a green brush and passes the handle of the brush to a single-line edit control in response to a WM_CTLCOLOR message:
static HBRUSH hbrGreen;
switch(msg) {
case WM_INITDIALOG:
/* Create a green brush */
hbrGreen = CreateSolidBrush(RGB(0, 255, 0));
return TRUE;
case WM_CTLCOLOR:
switch(HIWORD(lParam)) {
case CTLCOLOR_EDIT:
/* Set text to white and background to green */
SetTextColor((HDC) wParam, RGB(255, 255, 255));
SetBkColor((HDC) wParam, RGB(0, 255, 0));
return hbrGreen;
break;
case CTLCOLOR_MSGBOX:
/*
* For single-line edit controls, this code must be
* processed so that the background color of the format
* rectangle will also be painted with the new color.
*/
return hbrGreen;
}
return (HBRUSH) NULL;
}