ID Number: Q74043
3.00 3.10
WINDOWS
Summary:
The source code fragment below demonstrates how to paint the
foreground and background of a single-line edit control in an
application developed for the Microsoft Windows graphical environment.
It creates a window that contains one single-line edit control and
paints the edit control cyan on blue.
Windows sends the WM_CTLCOLOR message to the parent of an edit control
before the control is painted. If desired, the parent window, usually
a dialog box procedure, can process this message and change the text
and background colors of the control.
Note: Windows version 3.0 does not properly change the background
color of a single-line edit control. For more information on that
problem, query in the Microsoft Knowledge Base on the following words:
prod(winsdk) and WM_CTLCOLOR and single and edit
The following MainWndProc demonstrates WM_CTLCOLOR processing:
long FAR PASCAL MainWndProc(HWND hwnd, unsigned msg,
WORD wParam, LONG lParam)
{
static HWND hwndEdit;
static HBRUSH hBrush;
switch (msg)
{
case WM_CREATE:
{
HDC hdc;
TEXTMETRIC tm;
hdc = GetDC(hwnd);
GetTextMetrics(hdc, &tm);
ReleaseDC(hwnd, hdc);
hwndEdit = CreateWindow("edit", NULL,
WS_CHILD | WS_VISIBLE
| ES_AUTOHSCROLL | ES_MULTILINE,
10, 20, 100, tm.tmHeight,
hwnd, 1, hInst, NULL);
// Create a blue brush to be used for the edit control's
// background color.
hBrush = CreateSolidBrush(RGB(0, 0, 255));
}
break;
case WM_CTLCOLOR:
{
// Set foreground and background colors only if this
// is an edit control.
if (HIWORD(lParam) == CTLCOLOR_EDIT)
{
// Set the edit control's foreground text color to
// cyan and the text's background color to blue.
SetTextColor(wParam, RGB(0, 255, 255));
SetBkColor(wParam, RGB(0, 0, 255));
// Properly originate the background brush. This is
// of use if the brush is a pattern instead of a
// solid color.
UnrealizeObject(hBrush);
SetBrushOrg(wParam, 0, 0);
// Return a handle to the background brush for the edit
// control.
return (DWORD)hBrush;
}
}
break;
case WM_SETFOCUS:
SetFocus(hwndEdit);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
DeleteObject(hBrush);
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
Additional reference words: 3.00 3.10