Click here to Skip to main content
15,894,896 members
Articles / Programming Languages / C++

Add Color to Console Projects

Rate me:
Please Sign up or sign in to vote.
4.86/5 (67 votes)
14 Nov 2009CPOL2 min read 213.7K   6K   96  
With the use of a few support functions, color can be added to console applications running under Win32.
//------------------------------------------------------------------------------
//conColorDemo - main.cpp
//
// written in December 2004 by a JadedHobo
//
// This program demonstrates the use of colors
// in a Win32 console application.
//------------------------------------------------------------------------------

//-------------------------------------------------------------------<include>--
#include <iostream>
#include <conio.h>

//-------------------------------------------------------------------"include"--
#include "Console.h"

//------------------------------------------------------------------------main--
int main()
{
    using std::cout;
    using std::endl;
    using std::setw;
    using std::setiosflags;
    
    namespace con = JadedHoboConsole;
    
    cout << con::clr;
    cout << con::bg_blue << con::fg_white << setw( 40 )
         << setiosflags( std::ios::left )
         << "Funky colors in a console application" << endl;
         
         
    cout << con::bg_black << con::fg_white
         << "\nWhite   text on black background\n";
         
    cout << con::fg_gray    << "Gray    text on black background\n"
         << con::fg_red     << "Red     text on black background\n"
         << con::fg_green   << "Green   text on black background\n"
         << con::fg_blue    << "Blue    text on black background\n"
         << con::fg_cyan    << "Cyan    text on black background\n"
         << con::fg_magenta << "Magenta text on black background\n"
         << con::fg_yellow  << "Yellow  text on black background\n";
         
	cout << con::bg_gray << con::fg_yellow << "Press the 'any' key..."
         << con::bg_black << con::fg_white;
	int c = _getch();
    cout << "\nThe character " << con::bg_yellow << con::fg_black
         << static_cast< char >( c ) << con::bg_black << con::fg_white
         << " could be considered "
         << con::fg_green << "OK" << con::fg_white << " or "
         << con::fg_red   << "WRONG" << con::fg_white
         << " depending on taste\n" << endl;
         
    cout << con::bg_gray    << "White text on gray    background\n"
         << con::bg_red     << "White text on red     background\n"
         << con::bg_green   << "White text on green   background\n"
         << con::bg_blue    << "White text on blue    background\n"
         << con::bg_cyan    << "White text on cyan    background\n"
         << con::bg_magenta << "White text on magenta background\n"
         << con::bg_yellow  << "White text on yellow  background\n";
         
	cout << con::bg_white << con::fg_blue << "Press the 'any' key..."
         << con::bg_black << con::fg_white;
	_getch(); 
	return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions