Changing the Background Color of Edit Controls
This is the 2nd revision to the original article. I would like to thank everyone who posted comments and any answers to those comments that I've used in this latest revision.
If you need to change the background color of CEdit controls on your dialog, it is very simple ! (you don't need to derive your own class from CEdit).
The following example will change the appearance of specific CEdit controls including:
- Background color.
- Text color.
- Transparency.
I chose Blue and Red backgrounds with White text for this example.
In your CTestDlg header file, declare member variables from CBrush and COLORREF:
class CTestDlg : public CDialog
{
protected:
CBrush m_redbrush,m_bluebrush;
COLORREF m_redcolor,m_bluecolor,m_textcolor;
};
Then, add these lines in the OnInitDialog function:
BOOL CTestDlg::OnInitDialog()
{
m_redcolor=RGB(255,0,0); // red
m_bluecolor=RGB(0,0,255); // blue
m_textcolor=RGB(255,255,255); // white text
m_redbrush.CreateSolidBrush(m_redcolor); // red background
m_bluebrush.CreateSolidBrush(m_bluecolor); // blue background
}
Finally do this on the ID_CTLCOLOR handle:
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr;
switch (nCtlColor)
{
// process my edit controls by ID.
case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
switch (pWnd->GetDlgCtrlID())
{
// first CEdit control ID
case IDC_MYCONTROLNAME1: // put your own CONTROL ID
// here
pDC->SetBkColor(bluecolor); // change the background
// color [background colour
// of the text ONLY]
pDC->SetTextColor(textcolor); // change the text color
hbr = (HBRUSH) m_bluebrush; // apply the blue brush
// [this fills the control
// rectangle]
break;
// second CEdit control ID
case IDC_MYCONTROLNAME2: // make the TEXT transparent,
// but control is still
// filled with the brush
// color!
pDC->SetBkMode(TRANSPARENT); // make background
// transparent [only affects
// the TEXT itself]
pDC->SetTextColor(textcolor); // change the text color
hbr = (HBRUSH) m_redbrush; // apply the red brush
// [this fills the control
// rectangle]
break;
case IDC_MYCONTROLNAME3: // Make the text and the
// control rectangle
// transparent, so the
// dialog window
// color/bitmap draws
// through.
pDC->SetBkMode(TRANSPARENT); // make background
// transparent [only affects
// the TEXT itself]
pDC->SetTextColor(textcolor); // change the text color
hbr = m_brush; // Return handle to our
// CBrush object [this is
// the brush you've used to
// paint the dialog window.
break;
// otherwise, do default handling of OnCtlColor
default:
hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
break;
}
break;
// process Static text, READONLY controls, DISABLED * controls.
// * NOTE: Disabled controls can NOT have their text color
// changed.
// Suggest you change all your DISABLED controls to
// READONLY.
case CTLCOLOR_STATIC:
// process my edit controls by ID
switch (pWnd->GetDlgCtrlID())
{
// one of my READONLY edit controls (could also be disabled or
// static text)
case IDC_MYCONTROLNAME4:
pDC->SetBkColor(bluecolor); // change the background color
pDC->SetTextColor(textcolor); // change the text color
hbr = (HBRUSH) m_bluebrush; // apply the brush
break;
// otherwise, do default handling of OnCtlColor
default:
hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
}
break;
// otherwise, do default handling of OnCtlColor
default:
hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
}
return hbr; // return brush
}
I hope this code helps you as much as other articles on CodeGuru have helped me.
Miscellaneous Notes/Comments:
Windows CE, READONLY controls: [posted by Tony].
Code works great in EVC++/MFC app. However, for read-only edit boxes, use CTLCOLOR_BTN instead of CTLCOLOR_STATIC in the test of nCtlColor.
READONLY and DISABLED controls are processed using CTLCOLOR_STATIC!
If your controls are set to DISABLED, then you CAN NOT alter the text color; if you want this ability, change all your DISABLED controls to READONLY.
TRANSPARENCY: pDC->SetBkMode(TRANSPARENT;)DOES NOT MAKE THE WHOLE CONTROL TRANSPARENT! it only affects the text itself.
Think of it this way: The control rectangle is filled using the BRUSH, then the text is drawn on top using a background and foreground color. So, to make the entire control transparent, you need to use the above SetBkMode command, and also set the controls brush to the same brush you have used to draw the window itself.
Brief Example:
- Add a bitmap resource to your project.
- In your class header add a CBitmap variable, such as:
- In your class source, load the bitmap up in OnInitDialog:
- Add OnEraseBkgnd to your class:
- Write your custom OnCtlColor handler:
class CMyDialog : public Dialog
{
public:
CBitmap bitmapBackground;
...
BOOL CMyDialog::OnInitDialog()
{
bitmapBackground.Attach(Loadbitmap(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDB_BACKGROUND) ) );
...
BOOL CMyDialog::OnEraseBkgnd(CDC * pDC)
{
// center the bitmap in the window
CRect rect;GetClientRect(&rect);
CDC dc; dc.CreateCompatibleDC(pDC);
CBitmap * pOldBitmap = dc.SelectObject(&bitmapBackground);
BITMAP bitmap; bitmapBackground.GetObject(sizeof(BITMAP),
&bitmap);
int x=(rect.Width()-bm.bmWidth)/2+rect.left;
int y=(rect.Height()-bm.bmHeight)/2+rect.top;
pDC->BitBlt(x,y,bm.bmWidth,bm.bmHeight,&dc,0,0,SRCCOPY);
// center the bitmap
// you could stretch the bitmap to fill the window!
// pDC->StretchBlt(rect.left,rect.top,rect.Width(),
rect.Height(),&dc,0,0,bm.bmWidth,
bm.bmHeight,SRCCOPY);
dc.SelectObject(pOldBitmap);
pDC->SetBkMode(oldbackmode);
return TRUE;
}
HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd,
UINT nCtlColor)
{
HBRUSH hbr;
switch (nCtlColor)
{
// process my edit controls by ID.
case CTLCOLOR_EDIT:
case CTLCOLOR_MSGBOX:
switch (pWnd->GetDlgCtrlID())
{
// first CEdit control ID
case IDC_MYCONTROLNAME1:
// put your own CONTROL ID here
pDC->SetBkMode(TRANSPARENT); // make text
// background transparent
pDC->SetTextColor(RGB(255,0,0));
// change the text color to red.
hbr = (HBRUSH) GetStockObject(NULL_BRUSH);
// apply a null brush, so control's rectangle
// isn't filled.
break;
// otherwise, do default handling of OnCtlColor
default:
hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
}
return hbr; // return brush
}
Further Reading:
Colored/Blinking Controls and Dialogs with any Font by Yury Goltsman.
History:
Original article posted: March 13, 2001
Date Last Updated: May 19, 2003

Comments
How to change the background color of Combo Box?
Posted by srihariram on 06/06/2007 03:37amHi, Could any one suggest the way to change the background color and text color of Combo box. When i change the overload OnCtlColor, to change the background color of the dialog box, even the background color of the Combo box changes. how to overcome this. I am using VS2005 IDE.
ReplyHow to change the background color of Combo Box?
Posted by srihariram on 06/05/2007 08:19amHi, Could any one suggest the way to change the background color and text color of Combo box. When i change the overload OnCtlColor, to change the background color of the dialog box, even the background color of the Combo box changes. how to overcome this. I am using VS2005 IDE.
ReplyDifferent lines colour in edit box
Posted by Legacy on 03/17/2003 12:00amOriginally posted by: Eyal B
Well ,
ReplyIs it possible to colour lines in different colour ????
The code throws up unhandled exception
Posted by Legacy on 02/19/2003 12:00amOriginally posted by: MATT
hi the example was brilliant however it terminates my application after my dialog box is closed
throwing out an unhandled exception
does anyone know how to solve this please help.
ReplyChanging Bkgd color for Read-Only CEdit in Windows CE
Posted by Legacy on 01/28/2003 12:00amOriginally posted by: Tony
Code works great in EVC++/MFC app. However, for read-only edit boxes, use CTLCOLOR_BTN instead of CTLCOLOR_STATIC in the test of nCtlColor.
-
ReplyI want Source
Posted by palani_svk on 09/07/2004 07:56amNice Application
Replymaking this function work with BN_CLICKED Function
Posted by Legacy on 12/19/2002 12:00amOriginally posted by: Joe
I've been trying to change the colors of an edit box after a mouse click on a button, but I have no idea what function I would have to use
ReplyMany thanks for the feedback ... working on a updated article
Posted by Legacy on 12/13/2002 12:00amOriginally posted by: Duncan Weir
Thanks to you all for your feedback.
I am now working on an updated version of this article, with better explanations of how to get it working, and also to incorporate new features (As asked for), and workarounds for problems that you've already posted here in answer to other queries.
No ETA for completion, but hope to have it done before Xmas (2002).
ReplyHow to change text color in Windows CE
Posted by Legacy on 10/17/2002 12:00amOriginally posted by: Romin
How can we do this in Windows CE ?
ReplyThere is no ID_CTLCOLOR handle in Windows CE.
Please help..
Changing text color on the fly
Posted by Legacy on 10/16/2002 12:00amOriginally posted by: Jared
How can I adapt this code so that while the user is typing the text color changes (depending on certain characters near the start, ie. '/' - green text "//" - yellow ";2;" - orange
ReplyWhat about CEditView & ReadOnly
Posted by Legacy on 09/19/2002 12:00amOriginally posted by: Crercio O. Silva
Although I have seen many suggestions on dealign with multiline and ReadOnly CEdit I wonder if there is a way to change the background color of a CEditView derived class.
ReplyGreat work by the way.
Loading, Please Wait ...