Click here to Skip to main content
Click here to Skip to main content

Using colors in CEdit and CStatic

, 31 Mar 2001
Rate this:
Please Sign up or sign in to vote.
Classes derived from CEdit and CStatic. It makes changing colors for text and backgrounds easy.
<!-- Download Links --> <!-- Article image -->

Sample Image - ColorEdit_ColorStatic.jpg

<!-- Add the rest of your HTML here -->


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 your 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.

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

There are two ways you can associate your control id's 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)
	DDX_Control(pDX, IDC_ST_TEXT, m_stText);
	DDX_Control(pDX, IDC_EB_CTL, m_ebCtl);


BOOL CYourDlg::OnInitDialog()
    // TODO: Add extra initialization here

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 Smile | :)


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


About the Author

Robert Brault
Web Developer
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 5 PinmemberMember 1146415210-Apr-15 3:11 
GeneralMy vote of 5 PinmemberRainer Klopfer7-Sep-14 17:36 
GeneralMy vote of 5 PinmemberEricAtec12-Sep-12 6:22 
QuestionStill great after all this time Pinmemberferretchen21-Jun-12 16:27 
QuestionBackground color Pinmemberpippo pioppo22-Dec-11 0:26 
GeneralMy vote of 5 PinmemberOriolBayo21-Oct-11 1:03 
GeneralCannot open include file 'ColorEdit.h': No such file or directory ???? Pinmemberamarasat2-Jun-11 5:02 
GeneralRe: Cannot open include file 'ColorEdit.h': No such file or directory ???? PinmemberRobert Brault2-Jun-11 5:05 
GeneralRe: Cannot open include file 'ColorEdit.h': No such file or directory ???? Pinmemberamarasat2-Jun-11 5:27 
GeneralVery useful plug'n'play code :-) Pinmembercodenrico24-Feb-11 23:07 
GeneralMy vote of 5 Pinmembercodenrico24-Feb-11 23:05 
Generalthanks Pinmemberromantic_sjk30-Aug-10 23:16 
GeneralLicensing for this code Pinmemberjmanti4-May-10 8:57 
GeneralRe: Licensing for this code PinmemberRobert Brault30-Mar-11 4:14 
QuestionCColorStatic() PinmemberGillWest20-Apr-10 9:04 
GeneralThank you very much! PinmemberMichail25-Jan-10 0:17 
QuestionHow to change the color part of a MessageWindow Pinmemberafotiou17-Nov-08 2:39 
GeneralThank Pinmembernobihai26-Oct-08 6:23 
Generalthanks a lot Pinmemberliuzhiyong28-Sep-08 0:17 
GeneralAwesome - finally something that just worked out of the box. PinmemberGishu Pillai19-Nov-07 19:59 
QuestionHow to resize static control in DrawItem method Pinmemberrajnijain6-Sep-07 1:08 
GeneralBug int the program PinmemberAimin Liu26-Feb-07 22:29 
GeneralRe: Bug int the program PinmemberJSB2811-May-07 2:05 
QuestionBug in my utilisation Pinmemberbaerten2-Feb-07 6:11 
AnswerRe: Bug in my utilisation PinmemberRobert Brault2-Feb-07 6:22 
GeneralRe: Bug in my utilisation Pinmemberbaerten4-Feb-07 22:31 
QuestionAssertion errors Pinmemberconiaker21-Jan-07 21:38 
GeneralThanks! PinmemberSpitfireX12-Jun-06 3:26 
GeneralUnnessessary / Useless / Slowing code Pinmemberdarkzangel21-May-06 6:00 
AnswerRe: Unnessessary / Useless / Slowing code PinmemberSynetech24-May-06 5:03 
GeneralThank you very much PinmemberShangshu7-Mar-06 17:17 
GeneralThanks! PinmemberPauls217-Feb-06 12:30 
GeneralWhy not working in CPropertyPage Pinmembersbing0316-Feb-06 8:40 
GeneralRe: Why not working in CPropertyPage PinmemberRobert Brault16-Feb-06 11:25 
GeneralRe: Why not working in CPropertyPage Pinmembersbing0317-Feb-06 3:32 
JokeEven working on WindowsCE :-) PinmemberNitsanRachman31-Jan-06 21:03 
Questionwhy deconstruct function not deleteobject(cbrush)? Pinmemberbeelzebub9-Oct-05 19:49 
GeneralGood stuff PinmemberTony Reeves12-Apr-05 5:06 
Questionhow to change fonts in CEdit? PinmemberAdobe28-Jan-05 14:46 
AnswerRe: how to change fonts in CEdit? PinmemberRobert Brault28-Jan-05 17:38 
AnswerRe: how to change fonts in CEdit? Pinmembervikas amin23-Sep-05 5:31 
Questionhow to use this code for dynamically created controls Pinmemberfirstreflex20-Dec-04 3:53 
GeneralHelp required on bit more complex scenario Pinmemberfirstreflex16-Dec-04 11:04 
Generalselective color change Pinmemberdwb27-Oct-04 9:27 
GeneralThanks Pinmemberfastfootskater16-Feb-04 21:22 
GeneralThanks!! PinmemberUncle Sash15-Oct-03 11:17 
QuestionCan this work on CDialogBar??? Pinmembermichal_zu21-Nov-01 22:45 
QuestionHow to change the color of the border? PinmemberKK24-Oct-01 23:49 
AnswerRe: How to change the color of the border? PinmemberRobert Brault31-Oct-01 7:59 
AnswerRe: How to change the color of the border? PinmemberWizard_013-Jan-02 7:09 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 1 Apr 2001
Article Copyright 2001 by Robert Brault
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid