Microsoft KB Archive/243480

= How to change background color of a dialog box by using ATL =

Article ID: 243480

Article Last Modified on 1/17/2006

-

APPLIES TO

 Microsoft ActiveX Template Library 3.0, when used with:  Microsoft Visual C++ .NET 2002 Standard Edition

 Microsoft Visual C++ .NET 2003 Standard Edition

 Microsoft Visual C++ 2005 Express Edition 

-

<div class="notice_section">

This article was previously published under Q243480

<div class="notice_section">

Note Microsoft Visual C++ NET (2002) supported both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies to unmanaged Visual C++ code only.

Note Microsoft Visual C++ 2005 supports both the managed code model that is provided by the .NET Framework and the unmanaged native Windows code model.

<div class="summary_section">

SUMMARY
When creating dialog boxes in ATL 3.0, you can change the background color that is used without much effort. This article shows you how to change the background color for dialog boxes in ATL 3.0.

Basically, you need to handle the WM_CTLCOLORDLG message for the dialog box. You can do this by adding an entry for this message in the message map and returning the handle to new brush with desired color from this handler.

<div class="moreinformation_section">

MORE INFORMATION
Here are the steps for changing the color of a dialog box in ATL. <ol>  Add a member variable m_hDialogBrush of type HBRUSH to your dialog box class. HBRUSH m_hDialogBrush; </li>  Create brush with the desired color in dialog boxes constructor, and destroy it in the destructor. CColorDlg {     //this will create a blue brush //could use GetSysColorBrush instead if want to use a system brush m_hDialogBrush = CreateSolidBrush(RGB(0, 0, 255)); }

~CColorDlg {       DeleteObject(m_hDialogBrush); }                   </li>  Add a message map entry for WM_CTLCOLORDLG as shown in the following. BEGIN_MSG_MAP(CColorDlg) MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog) MESSAGE_HANDLER(WM_CTLCOLORDLG, OnCtlColorDlg) //Add this macro. COMMAND_ID_HANDLER(IDOK, OnOK) COMMAND_ID_HANDLER(IDCANCEL, OnCancel) END_MSG_MAP </li>  Add a OnCtlDialog member function to your dialog class like the code below. OnCtlDialog is handler for the WM_CTLCOLORDLG message. LRESULT OnCtlColorDlg(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) {       return (long) m_hDialogBrush; }                   </li></ol>

When the dialog box appears, it will have a blue background.

<div class="references_section">