MFC Tip: To fill the Background color of the Dialog and get the background of the controls to look correct

1. Handle the ON_WM_ERASEBKGND Message and write the following code

BOOL CTNV_MFCDialogDemoDlg::OnEraseBkgnd(CDC* pDC)
{
	CRect r;
	//GetClientRect gets the width & height of the client area of the Dialog
	GetClientRect(&r);
	CBrush br(RGB(0,255,0));
	pDC->SelectObject(br);
	pDC->FillRect(r,&br);

	//Make sure to return TRUE;
 //return CDialogEx::OnEraseBkgnd(pDC);
	return TRUE;
}
The output of the dialog is shown below 
2. To  get the background of the controls to look correct Handle the ON_WM_CTLCOLOR Message and write the following code - Make sure to return the Brush Handle which was created same as a color of Dialog Background.
HBRUSH CTNV_MFCDialogDemoDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	//HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 //return hbr;

 //Make sure to return the Brush color should be same as Dialog Background color
        CBrush br(RGB(0,255,0));
	return (HBRUSH)br;
}

The output of the dialog is shown below 




About these ads

One thought on “MFC Tip: To fill the Background color of the Dialog and get the background of the controls to look correct

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s