Click here to Skip to main content
15,881,687 members
Articles / Desktop Programming / MFC

Changing the Background Color of a Read-only Edit Control

Rate me:
Please Sign up or sign in to vote.
4.22/5 (37 votes)
18 Jan 20053 min read 170.9K   5.5K   37   25
An article on very easily changing the color of a read-only edit control

Introduction

This is my first article. So bear with me. I know it is a simple control, but I spent quite a lot of time figuring this out. I hope someone can read this and figure it out quicker.

The Problem

I needed a read-only text box. I created one using the CEdit control. The problem was that it had a grey background. There is no option or function to change it directly.

The Solution

I derived a class from CEdit called CReadOnlyEdit. I intercepted the background ON_WM_CTLCOLOR_REFLECT() message. This message's function looks like this:

C++
HBRUSH CReadOnlyEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	// TODO: Return a non-NULL brush if the parent's 
	//handler should not be called
	return NULL;
}

To change the background color of the EditBox, instead of returning NULL for the function above, I returned a brush with the color I wanted for the background. Also, if you notice in the function, above one of the parameters is a pointer to the Device Context (pDC) of the control. I used the pDC->SetTextColor(COLORREF rgb) function to change the text color. I then ran into a slight problem. The background changed to the correct color and the text did as well but the background of the text stayed white. This was a simple fix. I simply set the background color of the text using pDC->SetBkColor(COLORREF rgb) function. This is my modified function:

C++
HBRUSH CReadOnlyEdit::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	// TODO: Return a non-NULL brush if the parent's 
	//handler should not be called

	//set text color
	pDC->SetTextColor(m_crText);

	//set the text's background color
	pDC->SetBkColor(m_crBackGnd);

  //return the brush used for background this sets control background
  return m_brBackGnd;
}

As you can see, my control has three variables: COLORREF m_crText, COLORREF m_crBackGnd, and CBrush m_brBackGnd. I added two functions to my control, one to change the background color (and update the brush) and one to change the text color. Those two functions look like this:

C++
void CReadOnlyEdit::SetBackColor(COLORREF rgb)
{
	//set background color ref (used for text's background)
	m_crBackGnd = rgb;
	
	//free brush
	if (m_brBackGnd.GetSafeHandle())
       m_brBackGnd.DeleteObject();
	//set brush to new color
	m_brBackGnd.CreateSolidBrush(rgb);
	
	//redraw
	Invalidate(TRUE);
}
void CReadOnlyEdit::SetTextColor(COLORREF rgb)
{
  //set text color ref
  m_crText = rgb;

  //redraw
  Invalidate(TRUE);
}

The reason I have a COLORREF background variable is because to change the background color of the device context, I need a COLORREF. A problem I ran into was, the entire edit control was not being colored right away, this is why I added the Invalidate(TRUE) call to repaint the control.

That's all there was to it. By the way, my control doesn't set a background or text color by default. This can easily be done in the constructor.

Using the Code

Dialog Based

Create a CEdit control. Control double-click it. The Add Member Variable dialog appears. Choose Control for the Category and CEdit for the variable type. Set the variable name to something like m_wndEdit. When modifying the control, remember m_wndEdit is the control not a string containing the window text. To access the text, you will have to use the proper functions for the control (ie. CEdit::SetWindowText()).

Programmatically

The simplest way I can think of to use this is just change all the CEdit controls you want to be read-only to CReadOnlyEdit.

Changing the Colors

Change the background and text color of the control using the SetBack Color(COLORREF rgb) and SetTextColor(COLORREF rgb) functions. In the demo, I use a CColorDialog to get a color. The following is code from my demo. It is located in the Change Back Color button's click function:

C++
void CReadOnlyDlg::OnBack() 
{	
	// call color dialog and change background color
	CColorDialog dlg;
	if (dlg.DoModal() == IDOK)
		m_wndReadOnly.SetBackColor(dlg.GetColor());	
}

Where m_wndReadOnly is the CReadOnlyEdit control.

Note: My control doesn't set the read-only flag, you have to do this yourself in the CReadOnlyEdit::Create function or in the dialog control properties. Also, this can be done with the CEdit::SetReadOnly(BOOL) function.

History

  • Jan 21, 2005 - Update
    • Fixed bugs
    • Added SetBackColor and SetTextColor functions
  • Jan 18, 2005 - Posted
    • Basic control, changed background color to white

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Canada Canada
I am 20 years old and I live in Kitchener, Ontario. I have been programming since I was 10 years old. I started with QBasic, then up to Visual Basic, and finally C++. I am an avid programmer of Windows as well. Besides that, I am also interested in automation, PLCs, and electronics. I will be attending Conestoga College in Sept. 2005 for their Mechanical Engineering: Automation and Robotics program. I hope to someday have a job where I can incorporate my skills with programming Windows with the skills I learn in that program.

Comments and Discussions

 
Questionhello Kevin Bond Pin
wangandy9-Aug-11 3:02
wangandy9-Aug-11 3:02 
GeneralThanks Pin
youwithme28-Sep-10 15:22
youwithme28-Sep-10 15:22 
QuestionReadonly Textbox inside the GroupBox Control Pin
razhia11-Nov-09 16:34
razhia11-Nov-09 16:34 
GeneralMandou ver! Pin
L4U80Y9-Nov-09 8:11
L4U80Y9-Nov-09 8:11 
Generalhaha Pin
Alexander Mahone17-Dec-08 18:14
Alexander Mahone17-Dec-08 18:14 
GeneralMuch easier and quicker method Pin
mr. bleh30-Oct-07 9:45
mr. bleh30-Oct-07 9:45 
GeneralRe: Much easier and quicker method Pin
PaoloC00020-Nov-19 15:11
PaoloC00020-Nov-19 15:11 
GeneralThank you Pin
miniiiii29-Jul-07 18:40
miniiiii29-Jul-07 18:40 
GeneralThank you Pin
compex8-Jan-07 10:11
compex8-Jan-07 10:11 
QuestionHow to implement this in WTL Pin
CodeFundu17-Mar-06 23:46
CodeFundu17-Mar-06 23:46 
QuestiontextColor stays grayed when Ctrl is disabled Pin
FriendOfAsherah11-Mar-06 22:22
FriendOfAsherah11-Mar-06 22:22 
QuestionChaning the font in ReadOnlyEdit Pin
bcamp3-Mar-06 11:50
bcamp3-Mar-06 11:50 
QuestionHow does it work. Pin
CastleIsle17-Feb-06 8:05
CastleIsle17-Feb-06 8:05 
QuestionHow to make CReadOnlyEdit in MFC ClassWizard ? Pin
krya1-Feb-06 23:43
krya1-Feb-06 23:43 
AnswerRe: How to make CReadOnlyEdit in MFC ClassWizard ? Pin
CastleIsle17-Feb-06 7:29
CastleIsle17-Feb-06 7:29 
Generalmerci Pin
quicksoft15-Dec-05 19:45
quicksoft15-Dec-05 19:45 
GeneralAll this for what? Here is one liner to accomplish the same. Pin
the_gurudev29-Nov-05 18:07
the_gurudev29-Nov-05 18:07 
GeneralRe: All this for what? Here is one liner to accomplish the same. Pin
chionatech18-Dec-07 13:41
chionatech18-Dec-07 13:41 
Generalinitializing background- and textcolor without control variable Pin
mekanoo9-Sep-05 13:35
mekanoo9-Sep-05 13:35 
GeneralText Background Pin
Brian (BH)28-Jan-05 0:37
Brian (BH)28-Jan-05 0:37 
GeneralRe: Text Background Pin
bob169728-Nov-05 8:04
bob169728-Nov-05 8:04 
GeneralGreat first article! Pin
Hans Dietrich19-Jan-05 14:05
mentorHans Dietrich19-Jan-05 14:05 
QuestionHow to do it in c# Pin
nagarsoft19-Jan-05 10:08
nagarsoft19-Jan-05 10:08 
Have you got any idea on how I can do the same thing in C#?
Thanks a lot.
Andrea
AnswerRe: How to do it in c# Pin
W. Kleinschmit19-Jan-05 22:05
W. Kleinschmit19-Jan-05 22:05 
GeneralRe: How to do it in c# Pin
Anonymous2-Aug-05 4:42
Anonymous2-Aug-05 4:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.