MFC Library Reference 
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.

See Also

Reference

Handling Windows Messages in Your Dialog Box

Concepts

Life Cycle of a Dialog Box



Community Content

ddaS-edEn
Painting the Dialog background white
This should give you a dialog with a white background:


Add ON_WM_ERASEBKGND() to the MESSAGE_MAP. The message map will look something like this:


BEGIN_MESSAGE_MAP(MyDlg, CDialog)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

//}}AFX_MSG_MAP

ON_WM_ERASEBKGND()

END_MESSAGE_MAP()



Declare the OnEraseBkgnd() method inside you dialog class:


class MyDlg : public CDialog
{
// Construction
public:
MyDlg(CWnd* pParent = NULL); // standard constructor

// Dialog Data
enum { IDD = IDD_MY_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support






// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()

public:
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};



Write the OnEraseBkgnd() function definition in the CPP file:


BOOL MyDlg::OnEraseBkgnd(CDC* pDC)
{
CRect rect;
GetClientRect(&rect);
CBrush myBrush(RGB(255, 255, 255)); // dialog background color
CBrush *pOld = pDC->SelectObject(&myBrush);
BOOL bRes = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
pDC->SelectObject(pOld); // restore old brush
return bRes; // CDialog::OnEraseBkgnd(pDC);
}


That should do.


Page view tracker