Click here to Skip to main content
11,925,022 members (49,259 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked

Getting different font colors using the system CColorDialog and changing the background color

, 27 Oct 2002
Rate this:
Please Sign up or sign in to vote.
Believe it or not, changing the background color of a dialog box and using different color fonts is really easy. This will show you how to do so using the system dialog : CColorDialog.


Have you ever wanted to change the boring background of a dialog? How about give your controls better font colors? (really... black is way over used!) Well after hunting around, I have finally come up with VERY simple methods of doing this.

This article is intended to discuss two types of coloring. The background of the dialog and the font color.


We will start with the background.

Using the Class View, add a protected variable.

Type: CBrush
Name: m_brush

In your OnInitDialog add the following code.

m_brush.CreateSolidBrush(RGB(255,255,0)); //Yellow 

Now add a WM_CTLCOLOR message handle to get access to the color that we want. Smile | :) Now simply enter the following before the return hrb;

return m_brush;

Now run your program! It should be a bright yellow! Colorized backgrounds on Dialogs DO get annoying, so be careful!

Colorized Font

Next we come to the more practical part of the code. Colorizing fonts! First let's add some public variables using the Class View.

Name:  m_crColor

Type:  BOOL
Name:  m_bFirstTime

Second, make a button with a BN_CLICKED function (Use the Class Wizard) and call it something witty such as: OnChangeFonts(). Now were ready to edit your OnChangeFonts() function.

CColorDialog colorDlg;
if (colorDlg.DoModal() == IDOK) // The user selected the "OK" button
      m_crColor = colorDlg.GetColor(); 

OK, so what are we doing? First of all, were declaring what appears to be a VERY strange variable. Actually, it's really not all that strange. CColorDialog is a windows common dialog variable. You probably see windows common dialogs everyday. Chances are when you print, or do a page setup, or a number of other things you're using a windows common dialog.

Now we come to the if statement. First we tell the windows common dialog (in our case were using the pick a color one) to DoModal(). That means popup and don't let the user access any other dialogs in that program until they exit this one. Next we see if the user presses the "OK" button with the == IDOK) If they don't click "OK" we assume that means they don't really want to change colors.

Finally, with m_crColor = colorDlg.GetColor(); we set our COLORREF variable to the color that was picked. Now we are ready to set some variables in the OnInitDialog function.

m_bFirstTime = TRUE;

Now lets go back to the WM_CTLCOLOR message handle that we created earlier. Put the following code before the return m_brush


if (pWnd->GetDlgCtrlID() == IDC_LIST1 || pWnd->GetDlgCtrlID() == IDC_EDIT1) 
    // either the listbox or edit box has input focus 
    if (m_bFirstTime == TRUE) // First time through function... 
                              // set up default color 
    pDC->SetBkColor(RGB(255 - GetRValue(m_crColor), 
    255 - GetGValue(m_crColor), 255 - GetBValue(m_crColor))); 

First thing that we do is check to see if either the listbox or editbox have input focus. If one of them does, we next check to see if it's the first time for the function to run. We do this with the m_bFirstTime variable. If it is set to true, the text will be set to a default black. Otherwise, we update the color to be displayed.

RGB, if you haven't figured out yet, stands for the primary colors Red, Green, and Blue. Normally, you would specify a constant number of each, but we use variables in their place. GetXColor(COLORREF variable) is used to get the RBG values. Replace the X in "GetX" with R,G, or B. Our COLORREF variable is m_crColor.

Believe it or not, if you compiled and ran the program now, you would be able to pick font colors for the edit and listbox (assuming you used those controls...)! This all nice and good, except for, if you painted the background of your dialog, you have a ugly white "bubble" that surrounds your text. This is normally hidden if you don't paint your dialog, so you might take that into consideration.

So what do we do about that? Well, there is no real fix (I'm sure there is one, but I don't know about it), but we can be creative. The SetBkColor works just like SetTextColor and predictably, it changes that color of the "bubble". Now for the creative part. We could simply fill it with constants, and make it a color, but anytime a color got close to that it would be hard to see. So, instead we take 255 - RGB (RGB has a max of 255, so we can't go negative). I'm proud to say, that this gives you a neat effect, and simply prouder to say that it works (I spent too long trying to think of a way to solve the problem).

Lastly, go back to the OnChangeFont() function and set the m_bFirstTime to false:

m_crColor = colorDlg.GetColor();

m_bFirstTime = FALSE; // NEW LINE TO ADD!!

Well, were done! I hope this helps you!


  • May, 2002 Well, I put the code it the yellow boxes to make it easier to read. I also included a picture to help visualize what it looks like.
  • October, 2002 Just fixing some unclear things that have been bothering me. I hope this makes the article easier to read.


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

United States United States
I'm a 18 year old programmer who lives in hot Texas. I'm currently suffering through a Borland C++ class at my high school. I enjoy programming, winning chess, rowing, fishing, biking, and [sometimes] praticing the oboe. Feel free to e-mail me!

You may also be interested in...

Comments and Discussions

GeneralBug with numeric editboxes Pin
Tony Reynolds9-Dec-04 1:40
memberTony Reynolds9-Dec-04 1:40 
Generala realy good work Pin
mohsen nourian30-Nov-04 4:45
membermohsen nourian30-Nov-04 4:45 
Generaldialog Pin
Hamida30-Aug-04 18:16
memberHamida30-Aug-04 18:16 
GeneralRe: dialog Pin
cristitomi23-Mar-07 1:04
membercristitomi23-Mar-07 1:04 
GeneralThanks! And background color fix Pin
jim_henry22-Nov-03 21:38
memberjim_henry22-Nov-03 21:38 
GeneralRe: Thanks! And background color fix Pin
cristitomi23-Mar-07 1:08
membercristitomi23-Mar-07 1:08 
GeneralRegarding the state of the article Pin
Christian Graus13-Apr-02 12:22
memberChristian Graus13-Apr-02 12:22 
GeneralRe: Regarding the state of the article Pin
Selevercin13-Apr-02 13:25
memberSelevercin13-Apr-02 13:25 
Question??? Pin
Joshua Guy10-Apr-02 19:09
memberJoshua Guy10-Apr-02 19:09 
AnswerRe: ??? Pin
Rick York10-Apr-02 21:00
memberRick York10-Apr-02 21:00 
GeneralRe: ??? Pin
Joshua Guy10-Apr-02 21:13
memberJoshua Guy10-Apr-02 21:13 
GeneralRe: ??? Pin
Rick York10-Apr-02 22:49
memberRick York10-Apr-02 22:49 
GeneralRe: ??? Pin
Rick York15-Apr-02 10:44
memberRick York15-Apr-02 10:44 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151125.3 | Last Updated 28 Oct 2002
Article Copyright 2002 by Selevercin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid