Is there a way to output colored text to the console? I am using Visual Studio 2010, and only need the code to work in Windows.
I have been unsuccessful in finding anything except the windows COLOR command, but that changed the color for the entire screen, and I am looking for something that will change only the part I wish to output. I've seen it done in Managed C++
E.g.,
{color red}
cout << "Hello ";
{color blue}
cout << "world\n";
would yield "Hello world" in red and blue.
You can use the system("") command, which is used like that:
Coloring C++ output in Windows is done through SetConsoleTextAttribute, where the HANDLE of the console passed in along with attributes. However, calling SetConsoleTextAttribute is cumbersome. Fortunately, there are lots of small libraries on the internet and github that can assist, you should just pick one with an API you like. If you want to change colors with operator<<, I recommend this header-only library https://github.com/ikalnitsky/termcolor. The api looks like this:
If having to reset the color turns you off, try my library. It's header-only too, Windows only, and it lets you color printf statements easily: https://github.com/jrebacz/colorwin. The api looks like this:
I took this code from here:
Here is our in house solution:
and here are examples of colors to choose from: