Call CWinApp: SetDialogBkColor to change the background color of all applications. The first parameter specifies the background color, and the second parameter specifies the text color. In the following example, set the application dialog to blue background and yellow text.
BOOL CSampleApp: InitInstance ()
{
...
// Use blue dialog with yellow text.
SetDialogBkColor (RGB (0, 0,255), RGB (255,255, 0 ));
...
}
When you need to re-draw a dialog (or a sub-control of the dialog), Windows sends the message WM_CTLCOLOR to the dialog. Generally, you can ask Windows to select a paint brush for the background or reset the specified brush for the message. The following example describes how to create a red background dialog.
First, add the one-person member variable CBursh to the dialog base class:
Class CMyFormView: public CFormView
{
...
Private:
CBrush m _ brush; // background brush
...
};
Second, initialize the brush to the desired background color in the class constructor.
CMyFormView: CMyFormView ()
{
// Initialize background brush.
M_brush. CreateSolidBrush (RGB (0, 0,255 ))
}
Finally, use ClassWizard to process the WM_CTLCOLOR message and return a brush handle used to paint the conversation background. Note: The nCtlColor parameter must be checked because this function is also called when you redraw the dialog control.
HBRUSH CMyFormView: OnCtlColor (CDC * pDC, CWnd * pWnd, UINT nCtlColor)
{
// Determine if drawing a dialog box. If we are, return + handle
// Our own background brush. Otherwise let windows handle it.
If (nCtlColor = CTLCOLOR _ DLG)
Return (HBRUSH) m_brush. GetSafeHandle ();
Return CFormView: OnCtlColor (pDC, pWnd, nCtlColor );
}