Click here to Skip to main content
15,795,581 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi for changing the default background color of a dialog, i tried overriding it PreCreateWindow function:
BOOL CMyDialog::PreCreateWindow(CREATESTRUCT& cs)
	// TODO: Add your specialized code here and/or call the base class
	if (CDialog::PreCreateWindow(cs) == FALSE)
		return FALSE;

	CBrush br;
	VERIFY(br.CreateSolidBrush(RGB(251, 235, 202)));
	cs.lpszClass = AfxRegisterWndClass(0, ::LoadCursor(NULL, IDC_ARROW), br, NULL);

	return TRUE;
but it seems that DoModal doesn't cause it to be called.
what's ur suggestion?

spoon feeding the answer.

CngfhnDlg::CngfhnDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CngfhnDlg::IDD, pParent)
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_brush.CreateSolidBrush(RGB(0, 255, 0));

HBRUSH CngfhnDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
 HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

 // Return handle to our CBrush object
 hbr = m_brush;

 return hbr;
Share this answer
DrBones69 13-Mar-12 22:48pm    
This works for the initial color, but what about dynamically changing the color?
Maximilien 14-Mar-12 9:04am    
how about recreating the brush with a new color ?
(other than saying that it will never look good and is probably contrary to Microsoft's UX guideline )

Have a look at CWnd::OnCtlColor[^].
Share this answer
ilostmyid2 13-Mar-12 9:08am    
thank u :)
1. what's the problem with the code i wrote?!
2. it worked, but is it possible to setup a handler for WM_ERASEBKGND and call FillSolidRect there? if so, which one of the two ways is better?
Maximilien 13-Mar-12 14:25pm    
have you looked at CWnd::OnCtlColor ? this is what is used to change the background of a dialog...
Share this answer
ilostmyid2 13-Mar-12 13:18pm    

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900