Changing the Background and Static Text Color of your MFC Dialog box

By default, VC++ doesn’t have a backcolor or forecolor control properties unlike other visual programming languages such as VB or VFP. But you can change the dialog box text and back color by following these simple steps.

1. Start VC++ by clicking the start button>All Programs>Microsoft Visual Studio 6.0> Microsoft Visual C++ 6.0.
2. Click File>New>Select MFC AppWizard (exe)>Type the Project name>Click Ok>Select Dialog based>Click Finish.
3. Right click your dialog box>In the Class name list box>Select CprojectnameAPP>Double click the InitInstance member function.

The following codes will appear:

CBkColorApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CBkApp initialization
BOOL CBkColorApp::InitInstance()
{
AfxEnableControlContainer();

// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
//  the specific initialization routines you do not need.

#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic();// Call this when linking to MFC statically
#endif
4. Type the following before or after the line AfxEnableControlContainer(). Change the numbers inside the oval brace to create different background or text colors.
SetDialogBkColor(RGB(255,255,255),RGB(255,255,255));
5. The first RGB is for the backcolor and the other one is for the text.
6. Click the run icon to view the result.

Adding Colors to you C++ Program Output

There are several methods that can be used to add colors to your C++ program output but the simplest way is by using the system function. The system function allows you to pass commands to the operating system for execution. The syntax of using system varies in different programming languages. In C++, system function takes the following syntax:

system(“command”);
example:

system(“logoff”);
To add a color to your program output, use the color command which has the following syntax:

color bf
Were b is the background and f is the foreground.
Available colors for background and foreground are as follows:

0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
So if you want to have a black background with light green text, use:

system(“color oa”);

system function is typed inside your main function (or inside other functions) just after the opening curly brace and before the closing curly brace.

example:
#include<iostream>
using namespace std;
int main()
{
system(“Color 0a”);
cout<<”I am blue”;
cin.get();
}

The codes above produce a red text in black background. There are several things that you can do with the system function, and it’s up to you to discover. Good luck!