Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

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

0.00/5 (No votes)
27 Oct 2002 1  
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.

Introduction

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.

Background

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

Type:  COLORREF
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

UpdateData(TRUE); 

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->SetTextColor(RGB(0,0,0)); 
    else 
    { 
    pDC->SetTextColor(RGB(GetRValue(m_crColor),GetGValue(m_crColor), 
    GetBValue(m_crColor))); 
    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!

Updates

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

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