Click here to Skip to main content
6,306,412 members and growing! (22,361 online)
Email Password   helpLost your password?
Languages » C / C++ Language » Command line processing     Intermediate

Add color to your std::cout

By Vincent Godin

A tiny library that adds color to CRT programs.
C++, Windows, Visual Studio, STL, Dev
Posted:17 Nov 2006
Views:22,121
Bookmarked:49 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
27 votes for this article.
Popularity: 6.73 Rating: 4.70 out of 5
1 vote, 3.7%
1

2
1 vote, 3.7%
3
5 votes, 18.5%
4
20 votes, 74.1%
5

Introduction

This article illustrates a snippet that permits to add color to console messages.

Background

Well, does it really need any comments?

Using the code

This sample shows how to use the library:

#include "ConsoleColor.h"


std::cout << green << "This text is written in green" 
          << white << std::endl;
std::cout << color(FOREGROUND_RED|FOREGROUND_GREEN) 
          << "This text has a really exiting color !" 
          << white << std::endl;

Now here is the "library":

// ConsoleColor.h


#pragma once
#include <iostream>

#include <windows.h>


inline std::ostream& blue(std::ostream &s)
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE
              |FOREGROUND_GREEN|FOREGROUND_INTENSITY);
    return s;
}

inline std::ostream& red(std::ostream &s)
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hStdout, 
                FOREGROUND_RED|FOREGROUND_INTENSITY);
    return s;
}

inline std::ostream& green(std::ostream &s)
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hStdout, 
              FOREGROUND_GREEN|FOREGROUND_INTENSITY);
    return s;
}

inline std::ostream& yellow(std::ostream &s)
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hStdout, 
         FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
    return s;
}

inline std::ostream& white(std::ostream &s)
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hStdout, 
       FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
    return s;
}

struct color {
    color(WORD attribute):m_color(attribute){};
    WORD m_color;
};

template <class _Elem, class _Traits>
std::basic_ostream<_Elem,_Traits>& 
      operator<<(std::basic_ostream<_Elem,_Traits>& i, color& c)
{
    HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hStdout,c.m_color);
    return i;
}

// Copyleft Vincent Godin

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vincent Godin


Member

Occupation: Web Developer
Location: France France

Other popular C / C++ Language articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 7 of 7 (Total in Forum: 7) (Refresh)FirstPrevNext
GeneralYou shouldn't refere the r-value PinmemberEvil.Ghost18:22 13 Jan '09  
GeneralNot perfect, compiles sometimes PinmemberDzuka Automat5:01 26 Nov '08  
GeneralRe: Not perfect, compiles sometimes PinmemberEvil.Ghost18:17 13 Jan '09  
GeneralPerfect Pinmemberb3b9t21:23 31 Oct '08  
GeneralWhat did the parameter has been transfer? PinmemberKarlzheng17:03 22 Mar '07  
Generalperfect Pinmembercooleaf16:29 19 Dec '06  
GeneralRe: perfect Pinmemberzhangpingfly21:46 21 Dec '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Nov 2006
Editor: Smitha Vijayan
Copyright 2006 by Vincent Godin
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project