Total members 9950 | Gratitudes |It is currently Sat Feb 11, 2012 3:06 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Question
 Question subject: Add color to your std::cout
PostPosted: Sat Nov 08, 2008 11:35 pm 
Offline
Expert
User avatar

Joined: Tue Nov 06, 2007 2:17 pm
Posts: 847
Has thanked: 0 time
Have thanks: 1 time

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:

Code:
#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":
Collapse

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


 Article by:   Vincent Godin

_________________
Any help needed just reply to my topic ,
ccna ,ccnp certified .


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  


 Similar topics
 Topic title   Forum   Author   Comments 
 Changing the color and properties of the header tag  HTML examples  msi_333  0
 changing the background color for site main menu.  HTML examples  msi_333  0
 inherit background color from the parent element  HTML examples  msi_333  0
 Set border bottom color  CSS examples  msi_333  0
 Follow window background color  CSS examples  msi_333  0

All times are UTC [ DST ]


Users browsing similar posts

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Question | Next Question 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team