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.
OnInitDialog add the following code.
Now add a
WM_CTLCOLOR message handle to get access to the color that we want. :) Now simply enter the following before the
Now run your program! It should be a bright yellow! Colorized backgrounds on Dialogs DO get annoying, so be careful!
Next we come to the more practical part of the code. Colorizing fonts! First let's add some public variables using the Class View.
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
if (colorDlg.DoModal() == IDOK)
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.
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
m_bFirstTime = TRUE;
Now lets go back to the
WM_CTLCOLOR message handle that we created earlier. Put the following code before the
if (pWnd->GetDlgCtrlID() == IDC_LIST1 || pWnd->GetDlgCtrlID() == IDC_EDIT1)
if (m_bFirstTime == TRUE)
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
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;
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.