Colorizing text in the console with C++

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

Answer

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.

Leave a Reply

Your email address will not be published. Required fields are marked *