Aug 162008

Changing background color of a console application can be accomplished in two ways…

  1. Through console application API’s provided by Microsoft.
  2. Through color command in windows.

The second one is the easiest, let me show you how to do so…

// This will change the background to crimson and foreground color to white
const int NotUsed = system( "color 4F" );
int main()
  // Some code...
  return 0;

The color command works likewise. The first character, here ‘4’, is the background color and the second character, here ‘F’, is the foreground color. Both background and foreground can range from 0 to F.

Also note that to change only the foreground color call color command with one argument. system( “color A” ) call will change only the foreground color to “Bright green”. Note that if background color has been changed to something else other that the default one when your application was started, then background color on execution of above function call will default to that color.

Easiest way to try out the color command will be to use the command prompt, so open cmd and type in…

color 0F  -> Black background and white foreground

color 2A -> Dark green background and light green foreground

Further the help for the color command is available in windows and will be sufficient to help you out as its quite detailed, here is it…

C:\SomeUser>color /?
Sets the default console foreground and background colors.
COLOR [attr]
  attr        Specifies color attribute of console output

Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second the foreground.  Each digit
can be any of the following values:
    0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = White       F = Bright White
If no argument is given, this command restores the color to what it was
when CMD.EXE started.  This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the same.