Windows decides the foreground and background color of any window using current applied theme and color scheme. Windows programming does not provide any property to set the background color of any window widgets. But these are not rigid; programmer can alter these colors if needed. This is essential when programmer wants some control to have different color to highlight it before user or to give some notification to the user. This background property can be modified during handing specific window message called WM_CTLCOLORXXX. Windows sends WM_CTLCOLORXXX message just before drawing of the control. Thus Programmer can alter requited parameters while handing the message.

Message series number is WM_CTLCOLORXXX where XXX is the name of the call of the widget. Say for example for coloring EDIT box message is WM_CTLCOLOREDIT and thus for label/static window it will be WM_CTLCOLORSTATIC.

WM_CTLCOLORXXX : Windows sends these messages before drawing the control. HDC of the child window will be in wParam and HWND will be available in lParam. STATIC, BUTTON, EDIT, LIST, DLG, SCROLLBAR falls under these group. Followings are the name of individual messages.

  • WM_CTLCOLORMSGBOX (0x0132)
  • WM_CTLCOLOREDIT (0x0133)
  • WM_CTLCOLORLISTBOX (0x0134)
  • WM_CTLCOLORBTN (0x0135)
  • WM_CTLCOLORDLG (0x0136)
  • WM_CTLCOLORSCROLLBAR (0x0137)
  • WM_CTLCOLORSTATIC (0x0138)
WM_CTLCOLOREDIT 
hdc = (HDC) wParam;   // handle to display context 
hwnd = (HWND) lParam; // handle to control window

Parameters -
hdc - Value of wParam. Handle to the device context for the control window.
hwnd - Value of lParam. Handle to the window control.

Return Values
If an application processes this message, it must return the handle of a brush. The system uses the brush to paint the background of the edit control.

Default Action
The DefWindowProc function selects the default system colors for the edit control.

WM_CTLCOLORSTATIC and WM_CTLCOLOREDIT

  case WM_CTLCOLORSTATIC:
  {
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(0,0,255));
    SetBkColor(hdcStatic, RGB(250,250,0));
    return (INT_PTR)CreateSolidBrush(RGB(250,250,0));
  }
  case WM_CTLCOLOREDIT:
  {
    HDC hdcStatic = (HDC) wParam;
    SetTextColor(hdcStatic, RGB(0,0,255));
    SetBkColor(hdcStatic, RGB(0,230,0));
    return (INT_PTR)CreateSolidBrush(RGB(0,230,0));
  }

About our authors: Team EQA

Further readings

Where is WinMain() function in MFC application ?

MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.

What is the utility of CWinApp class?

This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...

Basic steps in Win32 GUI Application with source code.

Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.

What is a Window CallBack procedure and what is its utility?

DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.

What are LPARAM and WPARAM in window proc function?

LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.

What are the basic steps of a typical MFC based application?

We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...

#