Changing color of Specific Control on a dialog

This one guy asked me about how to change color of a specific control. I thought some of you might be interested in it too. 

Say you have lots of controls on a dialog and you want to change the color of only one Edit Box with IDC_EDIT ID.

Add this member variable to your class.

private:

CBrush m_bkBrush; 

 

Copy this line in constructor of your dialog class to create a solid brush. 

m_bkBrush.CreateSolidBrush( RGB( 0, 255, 0 ) ) ;

 

Override OnCtlColor and write this code.

HBRUSH CColorDlgDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 

{

CWnd* wnd = (CWnd*)GetDlgItem(IDC_NEW) ;

HBRUSH hbr = CDialog::OnCtlColor(pDC, wnd, nCtlColor);

  if (pWnd->GetDlgCtrlID() == IDC_NEW)

  {

     pDC->SetTextColor(RGB(0, 255, 0));

     pDC->SetBkColor(RGB(0, 0, 0));

     return (HBRUSH)(m_bkBrush.GetSafeHandle());

  }

return hbr;