Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 1.47 KB

setting-the-dialog-boxs-background-color.md

File metadata and controls

33 lines (26 loc) · 1.47 KB
description title ms.date helpviewer_keywords
Learn more about: Setting the Dialog Box's Background Color
Setting the Dialog Box's Background Color
11/17/2023
CSS, background attributes in styles [MFC]
HTML element formatting, background attributes
colors, dialog box
dialog boxes [MFC], colors
background colors, dialog boxes
MFC dialog boxes [MFC], colors

Setting the Dialog Box's Background Color

You can set the background color of your dialog boxes by handling WM_CTLCOLOR messages for the dialog box window. The color you set is used for only the specified dialog box.

For example, the following code fragment sets the background color of the dialog box to dark grey. The OnCtlColor member function is called whenever the dialog box is redrawn:

HBRUSH CAboutDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    return (HBRUSH)GetStockObject(DKGRAY_BRUSH);
}

For the previous code fragment to work:

  • add virtual HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor); to the protected: section of the class definition for your dialog.
  • add the following to the class definition for your dialog, and change CMyDialog to the name of your dialog class:
BEGIN_MESSAGE_MAP(CMyDialog, CDialogEx)
    ON_WM_CTLCOLOR()
END_MESSAGE_MAP()

See also

Working with Dialog Boxes in MFC
Handling Windows Messages in Your Dialog Box