c++ – windows cmd message color


Is it possible to set the color of the message that I will display on the command line using std::cout so that the color of all previous messages does not change (i.e. multi-colored text)?


Yes, this is possible with the SetConsoleTextAttribute function:

BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes)


hConsoleOutput — handle окна
wAttributes — набор атрибутов

Here here you can see for yourself the attributes.

In your case, handle can be obtained like this:

HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
Scroll to Top