使用 C++ 为控制台中的文本着色

2021-12-29 00:00:00 colors c++ colorize

如何使用 C++ 将彩色文本写入控制台?也就是说,如何用不同的颜色写出不同的文字?

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

推荐答案

为您的控制台文本

  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;
  }

字符属性以下是k"值的解释方式.

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

相关文章