Changing background color of an Edit Box

Add this member variable to your class.

private:

CBrush m_bkBrush; 

Now by using Class Wizard, add a new handler for WM_CTLCOLOR message. and write this code.

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

{

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

switch ( nCtlColor )

{

case CTLCOLOR_EDIT:

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

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

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

}

return hbr;

}

        

How to change the background color of a list box, message box, scroll bar, button, or static control?

Use these values as nCtlColor parameter in OnCtlColor function.

CTLCOLOR_LISTBOX   List-box control 

CTLCOLOR_MSGBOX   Message box

CTLCOLOR_SCROLLBAR   Scroll-bar control 

CTLCOLOR_STATIC   Static control 

CTLCOLOR_BTN button

}