Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Color console output with C++ in Windows

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.

like image 886
Rakosman Avatar asked Feb 13 '12 14:02

Rakosman


People also ask

How do I change the output color in Dev C++?

If you are going to write your program for Windows and you want to change color of text and/or background, use this: SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), attr); Where attr is a combination of values with | (bitwise OR operator), to choose whther you want to change foreground or background color.


2 Answers

I took this code from here:

// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// a Dev-C++ tested console application by vegaseat 07nov2004

#include <iostream>
#include <windows.h> // WinApi header

using namespace std; // std::cout, std::cin

int main()
{
HANDLE hConsole;
int k;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}

cin.get(); // wait
return 0;
}
like image 65
INS Avatar answered Oct 04 '22 23:10

INS


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:

using namespace termcolor;
std::cout << grey    << "grey message"    << reset << std::endl;
std::cout << red     << "red message"     << reset << std::endl;

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:

using namepsace wincolor;
std::cout << color(gray) << "grey message\n";
std::cout << color(red) << "red message\n";

std::cout << "normal color\n";
{
    withcolor scoped(red);
    std::cout << "|red\n";
    std::cout << "|red again\n";
}
std::cout << "normal color\n";
withcolor(cyan).printf("A cyan printf of %d\n", 1234);
like image 35
jrebacz Avatar answered Oct 04 '22 22:10

jrebacz