how to change color in c++ console code example

Example: c++ change console color

WORD color = 0x0F; // White
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "Hello World" << endl;
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), color);
// The farbcodes are the same as they, when you type "color help" in the windows 
// cmd in. First the number, then the letter (here the output of the cmd):
/*  0 = Black       8 = Gray
    1 = Blue        9 = Light Blue
    2 = Green       A = Light Green
    3 = Aqua        B = Light Aqua
    4 = Red         C = Light Red
    5 = Purple      D = Light Purple
    6 = Yellow      E = Light Yellow
    7 = Light Gray  F = White
    */
// So, to get the output white again, you have the set the color to:
// 0x + 0 (Black Background) + F (White Foreground)
// This works on every OS!

Tags:

Cpp Example