Tuesday, June 28, 2011

Change background color of a dialog box in VC++ MFC





// .H file
class CMyDlg : public CDialogEx
{
COLORREF m_clrBk = RGB(255,255,200); // assign the color as per requirement
public:
afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
};



// .CPP file
BEGIN_MESSAGE_MAP(CMyDlg, CDialogEx)
ON_WM_CTLCOLOR()
END_MESSAGE_MAP()

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
if(nCtlColor == CTLCOLOR_DLG) // remove if statement to change the background for all the controls
{
hbr = CreateSolidBrush(m_clrBk);
pDC->SetBkColor(m_clrBk);
}
return hbr;
}





No comments:

Post a Comment