Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print Coloured Text to Console in C++

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?

like image 369
Brock Woolf Avatar asked May 22 '09 18:05

Brock Woolf


People also ask

How do I print text in console color?

Syntax: System. out. println(ANSI_COLORNAME + "This text is colored" + ANSI_RESET);

How do I change my console color in C?

In order to make the text color red (number 31), you can write "\033[31m" which will make any following output red. If you want yellow text (33) on blue background (44), you write "\033[31;44m" . To reset everything back to the default colors, you write "\033[0m" . The terminal-colors.

How do I change text color in CPP?

If You want to change the Text color in C++ language There are many ways. In the console, you can change the properties of output. click this icon of the console and go to properties and change color. The second way is calling the system colors.


2 Answers

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();
}
like image 98
Skurmedel Avatar answered Oct 20 '22 11:10

Skurmedel


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

like image 23
ChrisF Avatar answered Oct 20 '22 13:10

ChrisF