Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC

Using Colors in CEdit and CStatic

Rate me:
Please Sign up or sign in to vote.
4.82/5 (84 votes)
31 Mar 20011 min read 375.3K   13.9K   121   56
Classes derived from CEdit and CStatic. It makes changing colors for text and backgrounds easy.

Sample Image - ColorEdit_ColorStatic.jpg

Introduction

I created these two classes to make changing the color of your Edit Box text and your Static text easy. I didn't need all the overhead of a CRichEditCtrl, but I did need to change the color of my text as well as the background color of the box. CStatic didn't have an easy way of changing the color of your text either.

These classes are derived from CEdit and CStatic.

How to Use

Include the files ColorEdit.cpp, ColorEdit.h and Color.h in your project if you are just working with Edit Boxes. If you want to incorporate colored static text also, you would add the files ColorStatic.cpp, ColorStatic.h.

In your dialogs header file, add :

C++
#include "ColorEdit.h"
#include "ColorStatic.h" //only if using colored static text.

public:
	CColorEdit m_ebCtl;
	CColorStatic m_stText; //only if using colored static text.

There are two ways you can associate your control ids with the classes. From now on, I will assume you are using both classes.

In your dialogs .cpp file, add:

C++
void YourDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CYourDlg)
	
	//}}AFX_DATA_MAP
	DDX_Control(pDX, IDC_ST_TEXT, m_stText);
	DDX_Control(pDX, IDC_EB_CTL, m_ebCtl);
}

or:

C++
BOOL CYourDlg::OnInitDialog()
{	
    // TODO: Add extra initialization here
    m_ebCtl.SubclassDlgItem(IDC_EB_CTL,this);
    m_stText.SubclassDlgItem(IDC_ST_TEXT,this);
}

Now that this is finished, it is time to use the class. There are three functions available for Edit Boxes and two for Static Text.

They are as follows:

C++
There are three functions available Currently:
SetBkColor(COLORREF crColor)    // Works for both classes
SetTextColor(COLORREF crColor)  // Works for both classes
SetReadOnly(BOOL flag = TRUE)   //This function is for CColorEdit only.

In the file Color.h is the following code:

C++
// Color.h
// Colorref's to use with your Programs

#define RED        RGB(127,  0,  0)
#define GREEN      RGB(  0,127,  0)
#define BLUE       RGB(  0,  0,127)
#define LIGHTRED   RGB(255,  0,  0)
#define LIGHTGREEN RGB(  0,255,  0)
#define LIGHTBLUE  RGB(  0,  0,255)
#define BLACK      RGB(  0,  0,  0)
#define WHITE      RGB(255,255,255)
#define GRAY       RGB(192,192,192)

These are just a few I picked out, but add as many colors as you need.

Here is how easy it is to use:

C++
m_ebCtl.SetTextColor(BLUE); //Changes the Edit Box text to Blue
m_ebCtl.SetBkColor(WHITE);  //By default your background color is the
                            //same as your system color(color of dialog)
m_ebCtl.SetReadOnly();      //This makes it so nobody can edit the text.
                            //If you disable the box it does not let you
                            //change colors.
m_stText.SetTextColor(RED); //Changes the Static Text to Red
m_stText.SetBkColor(GREEN); //You probably will not use it, but it's here.

I hope someone out there finds this useful :)

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
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalthanks for share Pin
claire chen1-Mar-17 16:13
claire chen1-Mar-17 16:13 
QuestionClass for CButton Pin
Apprieu (Apprieu)11-Dec-16 1:28
Apprieu (Apprieu)11-Dec-16 1:28 
GeneralMy vote of 5 Pin
Member 1146415210-Apr-15 2:11
Member 1146415210-Apr-15 2:11 
GeneralMy vote of 5 Pin
Rainer Klopfer7-Sep-14 16:36
Rainer Klopfer7-Sep-14 16:36 
GeneralMy vote of 5 Pin
EricAtec12-Sep-12 5:22
EricAtec12-Sep-12 5:22 
QuestionStill great after all this time Pin
ferretchen21-Jun-12 15:27
ferretchen21-Jun-12 15:27 
QuestionBackground color Pin
pippo pioppo21-Dec-11 23:26
pippo pioppo21-Dec-11 23:26 
GeneralMy vote of 5 Pin
OriolBayo21-Oct-11 0:03
OriolBayo21-Oct-11 0:03 
GeneralCannot open include file 'ColorEdit.h': No such file or directory ???? Pin
amarasat2-Jun-11 4:02
amarasat2-Jun-11 4:02 
GeneralRe: Cannot open include file 'ColorEdit.h': No such file or directory ???? Pin
Robert Brault2-Jun-11 4:05
Robert Brault2-Jun-11 4:05 
GeneralRe: Cannot open include file 'ColorEdit.h': No such file or directory ???? Pin
amarasat2-Jun-11 4:27
amarasat2-Jun-11 4:27 
GeneralVery useful plug'n'play code :-) Pin
codenrico24-Feb-11 22:07
codenrico24-Feb-11 22:07 
GeneralMy vote of 5 Pin
codenrico24-Feb-11 22:05
codenrico24-Feb-11 22:05 
Generalthanks Pin
romantic_sjk30-Aug-10 22:16
romantic_sjk30-Aug-10 22:16 
GeneralLicensing for this code Pin
jmanti4-May-10 7:57
jmanti4-May-10 7:57 
GeneralRe: Licensing for this code Pin
Robert Brault30-Mar-11 3:14
Robert Brault30-Mar-11 3:14 
QuestionCColorStatic() Pin
GillWest20-Apr-10 8:04
GillWest20-Apr-10 8:04 
In the constructor for this class you have three lines of code:

m_crBkColor = ::GetSysColor(COLOR_3DFACE); // Initializing the Background Color to the system face color.
	m_crTextColor = BLACK; // Initializing the text to Black
	m_brBkgnd.CreateSolidBrush(m_crBkColor); // Create the Brush Color for the Background.


Can you explain why if this code is commented out the text color isnt displayed. And the brush is set to the background color. I assumed the message was passed from the dialog to the control but why does the background brush color matter?

Thank You the code was easy to follow and it works well for my project but I just want to know why I'm doing something thank you again
GeneralThank you very much! Pin
Michail24-Jan-10 23:17
Michail24-Jan-10 23:17 
QuestionHow to change the color part of a MessageWindow Pin
afotiou17-Nov-08 1:39
afotiou17-Nov-08 1:39 
GeneralThank Pin
nobihai26-Oct-08 5:23
nobihai26-Oct-08 5:23 
Generalthanks a lot Pin
liuzhiyong27-Sep-08 23:17
liuzhiyong27-Sep-08 23:17 
GeneralAwesome - finally something that just worked out of the box. Pin
Gishu Pillai19-Nov-07 18:59
Gishu Pillai19-Nov-07 18:59 
QuestionHow to resize static control in DrawItem method Pin
rajnijain6-Sep-07 0:08
rajnijain6-Sep-07 0:08 
GeneralBug int the program Pin
Aimin Liu26-Feb-07 21:29
Aimin Liu26-Feb-07 21:29 
GeneralRe: Bug int the program Pin
JSB2811-May-07 1:05
JSB2811-May-07 1:05 

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.