Colorizing text in the console with C++

后端 未结 12 2621
礼貌的吻别
礼貌的吻别 2020-11-27 10:04

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

12条回答
  •  情书的邮戳
    2020-11-27 10:52

    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.

提交回复
热议问题