views:

1610

answers:

3

I would like to write a Console class that can output coloured text to the console.

So I can do something like (basically a wrapper for printf):

Console::Print( "This is a non-coloured message\n" );
Console::Warning( "This is a YELLOW warning message\n" );
Console::Error( "This is a RED error message\n" );

How would I print different coloured text to the Windows Console?

+1  A: 

I did a search for "c++ console write colored text" and came up with this page at about 4 or 5. As the site has a copy & paste section I thought I'd post it here (another question on link rot also prompted this):

#include <stdlib.h>
#include <windows.h>
#include <iostream>

using namespace std;

enum Color { DBLUE=1,GREEN,GREY,DRED,DPURP,BROWN,LGREY,DGREY,BLUE,LIMEG,TEAL,
    RED,PURPLE,YELLOW,WHITE,B_B };
/* These are the first 16 colors anyways. You test the other hundreds yourself.
   After 15 they are all combos of different color text/backgrounds. */

bool quit;

void col(unsigned short color)
{
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}

istream &operator>> ( istream &in, Color &c )
{
    int tint;
    cin >> tint;
    if (tint==-1) quit=true;
    c=(Color)tint;
}

int main()
{
    do {
        col(7); // Defaults color for each round.
        cout << "Enter a color code, or -1 to quit... ";
        Color y;
        cin >> y; // Notice that >> is defined above for Color types.
        col(y); // Sets output color to y.
        if (!quit) cout << "Color: " << (int)y << endl;
    } while (!quit);
    return 0;
}

For C# there's this page

ChrisF
That first link you had was informative even though it was C#.
sean e
The search term was "c# console.write colored text" in that case
ChrisF
+4  A: 

Check out this guide. I would make a custom manipulator so I could do something like:

std::cout << "standard text" << setcolour(red) << "red text" << std::endl;

Here's a small guide on how to implement your own manipulator.

A quick code example:

#include <iostream>
#include <windows.h>
#include <iomanip>

using namespace std;

enum colour { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };

struct setcolour
{
   colour _c;
   HANDLE _console_handle;


       setcolour(colour c, HANDLE console_handle)
           : _c(c), _console_handle(0)
       { 
           _console_handle = console_handle;
       }
};

// We could use a template here, making it more generic. Wide streams won't
// work with this version.
basic_ostream<char> &operator<<(basic_ostream<char> &s, const setcolour &ref)
{
    SetConsoleTextAttribute(ref._console_handle, ref._c);
    return s;
}

int main(int argc, char *argv[])
{
    HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);
    cout << "standard text" << setcolour(RED, chandle) << " red text" << endl;

    cin.get();
}
Skurmedel
This code doesn't actually work, your previous version that you just replaced however did. Ill double check i have it right though
Brock Woolf
@Brock Woolf: Yes, sorry. I had a problem in the copying of the HANDLE.
Skurmedel
Wonderful that works perfectly. Thank you! :D
Brock Woolf
+1  A: 

Look here to find out how to write to the console in Windows and here how to apply attributes on it.
What OS are you working on?

the_drow