Colorizing text in the console with C++

后端 未结 12 2611
礼貌的吻别
礼貌的吻别 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:58

    On Windows 10 you may use escape sequences this way:

    #ifdef _WIN32
    SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), ENABLE_VIRTUAL_TERMINAL_PROCESSING);
    #endif
    // print in red and restore colors default
    std::cout << "\033[32m" << "Error!" << "\033[0m" << std::endl;
    

提交回复
热议问题