Click here to Skip to main content
14,336,087 members

Using Colors in CEdit and CStatic

Rate this:
4.80 (80 votes)
Please Sign up or sign in to vote.
4.80 (80 votes)
31 Mar 2001
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 :

#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:

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:

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:

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:

// 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:

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.

Share

About the Author

Robert Brault
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
QuestionBug in my utilisation Pin
baerten2-Feb-07 5:11
memberbaerten2-Feb-07 5:11 
AnswerRe: Bug in my utilisation Pin
Robert Brault2-Feb-07 5:22
memberRobert Brault2-Feb-07 5:22 
GeneralRe: Bug in my utilisation Pin
baerten4-Feb-07 21:31
memberbaerten4-Feb-07 21:31 
QuestionAssertion errors Pin
coniaker21-Jan-07 20:38
memberconiaker21-Jan-07 20:38 
GeneralThanks! Pin
SpitfireX12-Jun-06 2:26
memberSpitfireX12-Jun-06 2:26 
GeneralUnnessessary / Useless / Slowing code Pin
darkzangel21-May-06 5:00
memberdarkzangel21-May-06 5:00 
AnswerRe: Unnessessary / Useless / Slowing code Pin
Synetech24-May-06 4:03
memberSynetech24-May-06 4:03 
GeneralThank you very much Pin
Shangshu7-Mar-06 16:17
memberShangshu7-Mar-06 16:17 
GeneralThanks! Pin
Pauls217-Feb-06 11:30
memberPauls217-Feb-06 11:30 
GeneralWhy not working in CPropertyPage Pin
sbing0316-Feb-06 7:40
membersbing0316-Feb-06 7:40 
GeneralRe: Why not working in CPropertyPage Pin
Robert Brault16-Feb-06 10:25
memberRobert Brault16-Feb-06 10:25 
GeneralRe: Why not working in CPropertyPage Pin
sbing0317-Feb-06 2:32
membersbing0317-Feb-06 2:32 
JokeEven working on WindowsCE :-) Pin
NitsanRachman31-Jan-06 20:03
memberNitsanRachman31-Jan-06 20:03 
Questionwhy deconstruct function not deleteobject(cbrush)? Pin
beelzebub9-Oct-05 18:49
memberbeelzebub9-Oct-05 18:49 
GeneralGood stuff Pin
Tony Reeves12-Apr-05 4:06
memberTony Reeves12-Apr-05 4:06 
Questionhow to change fonts in CEdit? Pin
Adobe28-Jan-05 13:46
memberAdobe28-Jan-05 13:46 
AnswerRe: how to change fonts in CEdit? Pin
Robert Brault28-Jan-05 16:38
memberRobert Brault28-Jan-05 16:38 
AnswerRe: how to change fonts in CEdit? Pin
vikas amin23-Sep-05 4:31
membervikas amin23-Sep-05 4:31 
Questionhow to use this code for dynamically created controls Pin
firstreflex20-Dec-04 2:53
memberfirstreflex20-Dec-04 2:53 
GeneralHelp required on bit more complex scenario Pin
firstreflex16-Dec-04 10:04
memberfirstreflex16-Dec-04 10:04 
Generalselective color change Pin
dwb27-Oct-04 8:27
memberdwb27-Oct-04 8:27 
GeneralThanks Pin
Fastfootskater16-Feb-04 20:22
memberFastfootskater16-Feb-04 20:22 
GeneralThanks!! Pin
Uncle Sash15-Oct-03 10:17
memberUncle Sash15-Oct-03 10:17 
QuestionCan this work on CDialogBar??? Pin
michal_zu21-Nov-01 21:45
membermichal_zu21-Nov-01 21:45 
QuestionHow to change the color of the border? Pin
KK24-Oct-01 22:49
memberKK24-Oct-01 22:49 

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.

Article
Posted 28 Mar 2001

Stats

336.2K views
12.7K downloads
115 bookmarked