Colorizing text in the console with C++

Sudantha  picture Sudantha · Oct 29, 2010 · Viewed 249.2k times · Source

How can I write colored text to the console with C++? That is, how can I write different text with different colors?

Answer

Sheen picture Sheen · Oct 29, 2010

Add a little Color to your Console Text

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

alt text

Character Attributes Here is how the "k" value be interpreted.