Is it possible to set the color of the message, which I will display on the command line using std::cout
, so that the color of all previous messages does not change (that is, multi-colored text)?
1 answer
Yes, this is possible using the SetConsoleTextAttribute
function:
BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes)
Where:
hConsoleOutput — handle окна wAttributes — набор атрибутов
Here you can see the attributes themselves.
In your case, the handle
can be obtained like this:
HANDLE hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
|
SetConsoleTextAttribute
color to the desired one and before that you memorize the old color, then output the necessary characters to the console and thenSetConsoleTextAttribute
with the "old" color again. - DuracellSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
- zergon321