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

  14 Responses to “Changing background color of a console application?”

  1. thanks a lot

  2. I LOVE YOU SO MUCH!!! THANK YOU FOR THIS LITTLE PIECE OF CODE, I HAVE LOOKED ALL OVER THE WEB!!! 😀

  3. wow
    thank you very much
    it can help me to make my program look nicer
    (^^)

  4. Woow thanks dude… I’ve pass about 2 hours looking for that simple code, i just needed to add color to my console background!!!!!!!!!thanks!!!!!!!!!!

  5. Thanks man thanks for the suggestion.

  6. Thanks will do that & keep blogging

  7. Hi Binny, maybe you can write a custom clear screen function which prints out spaces with a given background color using above functions.

  8. Now i have used those functions but they only change the background of the text not the console background.can u suggest anything else

    Best regards

  9. Hi Binny,

    As far as your question is concerned in turbo c++ you need to use textcolor and textbackground functions. Make sure you call clrscr after you set color for immediate effect.

  10. Thanks a lot Nibu
    i was looking for code to change the colour & yours works well.
    But can you give me code to change the console colour in a dos
    environment.I use an old turbo c++ compiler eventhough i use
    visual c++ since my college has only that.
    ps. where do ulive.u seem like a malayali like me.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.