Changing Background Colors
To change the background color of a dialog box or a main window requires 2 steps.
1.   Invalidate the window after setting a global brush to the desired color.   This can be
      encapsulated in the attribute field of the window data structure.
2.   Process in the appropriate handler.


When a dialog box background needs updating a WM_CtlColorDlg message is sent.  This is intercepted in OS.Windows.Dialogs.OnColorAction.  The brush is selected into the context device of the dialog box.  OnColorAction.BackgroundBrush parameter must be set to the value of the brush handle, otherwise the new color will not take effect. The action is embedded in OnColorAction rather than in ColorHandler is to ensure that setting the background color through OS.Dialogs.SetBackgroundColor always work.  To override this default, derived ColorHandler with the value of the BackgroundBrush parameter is set to the new value to the brush handle, otherwise the old value set by by the BackgroundColor parameter of OS.Windows.Dialogs.RunModal, OS.Windows.Dialogs./RunModeless and OS.Windows.Dialogs.SetBackgroundColor will prevail..

There are 2 approaches to chaning the background color of a main window..  The simpler way is to change the brush of the window class.  This is done by a call to OS.Windows.Ops.SetBackgroundBrush which essentially sets the new color to the global brush handle (again encapsulated in the attribute field to avoid it being declared globally) and invalidates the entire window to cause repainting.  The problem is that it changes the background colors of all windows belonging to the same class.  The second method eliminates this problem.  Instead the window background is painted when WM_Erase_Background is sent.  This is caused by invalidating the window in SetBackgroundColor.  The client area of the window is then filled with the color of the global brush using FillRect.

Home

Changing Control Colors