Home > Windows API > Changing background color of a console application?

Changing background color of a console application?

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.
  1. Alex
    April 30, 2009 at 2:22 pm


  2. eky
    April 28, 2009 at 7:50 am

    thank you very much
    it can help me to make my program look nicer

  3. cram
    March 30, 2009 at 3:26 am

    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!!!!!!!!!!

  4. Supratik
    January 23, 2009 at 5:52 am

    Thanks man thanks for the suggestion.

  5. Binny
    October 2, 2008 at 2:54 am

    Thanks will do that & keep blogging

  6. October 1, 2008 at 4:31 pm

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

  7. Binny
    October 1, 2008 at 4:14 pm

    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

  8. October 1, 2008 at 7:51 am

    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.

  9. Binny
    October 1, 2008 at 7:38 am

    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.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: