Reputation: 433
I have been searching for days to figure out how to change a control's background color. The most logical solution I found is here:
I implemented this tutorial as follows:
class Cbackgroundcolor_mfc_testDlg : public CDialogEx
{
public:
Cbackgroundcolor_mfc_testDlg(CWnd* pParent = NULL);
enum { IDD = IDD_BACKGROUNDCOLOR_MFC_TEST_DIALOG };
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
CBrush m_redbrush,m_bluebrush; //<---- here
COLORREF m_redcolor,m_bluecolor,m_textcolor; //<---- here
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
public:
CStatic m_staticTODO;
protected:
// | | | and here
// v v v
HBRUSH Cbackgroundcolor_mfc_testDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
switch (nCtlColor)
{
case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
switch (pWnd->GetDlgCtrlID())
{
case IDC_STATICTODO:
pDC->SetBkColor(m_bluecolor);
hbr = (HBRUSH) m_bluebrush;
break;
}
default:
hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
}
return hbr;
}
};
I also added the necessary lines to OnInitDialog().
As far as I can tell, I have implemented the tutorial properly and yet the control background colors are still not changing. Can someone help me figure out what else I need to do to change the background color of a control?
Upvotes: 0
Views: 1067
Reputation: 10415
OnCtlColor is a message handler for WM_CTLCOLOR. You need to add this message to the dialog's message map to get the function called. Add this line inside the message map:
ON_WM_CTLCOLOR()
Upvotes: 1