Click here to Skip to main content
12,242,431 members (48,516 online)
Click here to Skip to main content
Add your own
alternative version

Stats

82.4K views
88 bookmarked
Posted

Add color to your std::cout

, 17 Nov 2006
Rate this:
Please Sign up or sign in to vote.
A tiny library that adds color to CRT programs.

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

Share

About the Author

Vincent Godin
Web Developer
France France
No Biography provided

You may also be interested in...

Comments and Discussions

 
NewsI wrote a similar, but more... Pin
chkesp8-Nov-14 21:35
memberchkesp8-Nov-14 21:35 
GeneralMy vote of 4 Pin
Vijay_vijay14-Aug-14 21:40
memberVijay_vijay14-Aug-14 21:40 
GeneralMy vote of 3 Pin
Throwaway1113-Jun-14 3:43
memberThrowaway1113-Jun-14 3:43 
GeneralMy vote of 1 Pin
Throwaway1113-Jun-14 3:42
memberThrowaway1113-Jun-14 3:42 
GeneralIt is not x-platform Pin
Member 108614353-Jun-14 3:38
memberMember 108614353-Jun-14 3:38 
GeneralMy vote of 4 Pin
john_th26-Mar-13 21:21
memberjohn_th26-Mar-13 21:21 
GeneralMy vote of 5 Pin
Ahmed Charfeddine13-Aug-12 16:14
memberAhmed Charfeddine13-Aug-12 16:14 
GeneralYou shouldn't refere the r-value Pin
Evil.Ghost13-Jan-09 18:22
memberEvil.Ghost13-Jan-09 18:22 
GeneralRe: You shouldn't refere the r-value Pin
daihouxiang13-May-10 2:38
memberdaihouxiang13-May-10 2:38 
GeneralNot perfect, compiles sometimes Pin
Dzuka Automat26-Nov-08 5:01
memberDzuka Automat26-Nov-08 5:01 
GeneralRe: Not perfect, compiles sometimes Pin
Evil.Ghost13-Jan-09 18:17
memberEvil.Ghost13-Jan-09 18:17 
SuggestionRe: Not perfect, compiles sometimes Pin
ArunMBharadwaj29-Nov-12 0:51
memberArunMBharadwaj29-Nov-12 0:51 
GeneralPerfect Pin
b3b9t31-Oct-08 21:23
memberb3b9t31-Oct-08 21:23 
QuestionWhat did the parameter has been transfer? Pin
Karlzheng22-Mar-07 17:03
memberKarlzheng22-Mar-07 17:03 
Generalperfect Pin
cooleaf19-Dec-06 16:29
membercooleaf19-Dec-06 16:29 
GeneralRe: perfect Pin
zhangpingfly21-Dec-07 21:46
memberzhangpingfly21-Dec-07 21:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 17 Nov 2006
Article Copyright 2006 by Vincent Godin
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid