Creating Background Colors on Windows and Dialogs

Introduction

Sometimes I would like to get away from the Borland gray dialogs with the white listboxes. This describes how to create a colored background (you can change) and a listbox with a gray (light gray in this case by whatever color you want) background. Here is such an example.

Coloring A Window

There are four things you have to do to color a window. They are:

  1. Add a pointer to a TBrush to the windows class and initialize it in the SetUpWindow function. This is accomplished by _myBrush = new TBrush(TColor::yourcolor);
  2. Add the standard virtual function CleanupWindow in which you delete _myBrush.
  3. Add the event hander EvCtlColor to handle the WM_CTRCOLOR message. This is boiler plate given here.
    HBRUSH ColorMe::EvCtlColor (HDC dc, HWND hWndChild, uint ctlType)
    {
    	 HBRUSH result;
    
    	 result = TDialog::EvCtlColor(dc, hWndChild, ctlType);
    
    	 // INSERT>> Your code here.
    	 if (ctlType == CTLCOLOR_DLG)
    		result = *_myBrush;     
    	 return result;
    }
  4. Finally add the WM_PAINT handler.

Complete Code

To get the complete code for this program click here.