INF: Changing the Background Color in an MFC Dialog Box

ID Number: Q85517

1.00

WINDOWS

docerr

Summary:

Dialog boxes created using the Microsoft Foundation Classes (MFC) from

Microsoft C/C++ version 7.0 are displayed with a gray background by

default. In some cases, it is desirable to change this color.

The member function SetCtlBkColor of the CDialog class can be used to

change the background color of the dialog box (and any static controls

in the dialog) to a different color. This function is not documented

in the online help or the printed documentation; however, the source

code can be found in the WINDOW.CPP MFC source file. The use of this

function is explained below.

More Information:

The prototype of SetCtlBkColor is listed in the AFXWIN.H header file

as follows:

BOOL CDialog::SetCtlBkColor( COLORREF clrCtlBk );

To use this function, call it in your dialog box's OnInitDialog member

function. The parameter should be a COLORREF describing the new

background color of the dialog box. The return value will be TRUE if

the function successfully changed the background color and FALSE if

the background color could not be changed. If you call the

SetCtlBkColor function after the dialog is displayed, the new color

will take effect only on the next dialog box repaint.

For your dialog box to be displayed with a consistent background color

for all the controls, make sure that the RGB value passed in as the

COLORREF argument is a solid color rather than a dithered color. If

you do use a dithered color in your SetCtlBkColor call, the dialog box

background will be painted with this dithered color, while the text

background will be painted with the nearest solid color.

One method of ensuring that you always get a solid color is to use the

member function GetNearestColor of the CDC class. GetNearestColor

finds the closest solid color available for a given DC. The following

code fragment shows how to use this function in conjunction with the

SetCtlBkColor function:

// CMyAboutBox class was derived from CModalDialog

BOOL CMyAboutBox::OnInitDialog()

{

CClientDC dc(this); // Get DC to dialog box

// Set dialog color to a nice light blue

SetCtlBkColor(dc.GetNearestColor(RGB(100,100,255)));

}

In addition to changing the background colors, the SetCtlBkColor

function also allows you to disable the background coloring

functionality of an MFC dialog box. By disabling the background

coloring, the standard "window background color" (as set by the

control panel) will be used to color the background of the dialog box.

To disable background coloring, delete the brush handle contained in

the CDialog m_hBrushCtlBk member and set it to NULL. Then, call

SetCtlBkColor with an argument of -1.

The following code fragment demonstrates the steps required to disable

the background coloring for a CDialog:

BOOL CMyAboutBox::OnInitDialog()

{

if (m_hBrushCtlBk != NULL)

{

::DeleteObject( m_hBrushCtlBk ); // Delete the brush

m_hBrushCtlBk = NULL; // Set it to NULL

}

// This call will disable all background coloring for the dialog

SetCtlBkColor( -1 );

}

Additional reference words: grey 1.00 7.00