【问题标题】:Changing Color of Text In C++ Console在 C++ 控制台中更改文本颜色
【发布时间】:2020-10-06 13:56:17
【问题描述】:

我正在尝试在 C++ 中更改我的文本颜色,此时我已经在互联网上搜索了。谁能帮助我我尝试使用“#include”,但这仍然不起作用。

我发誓我总是忘记包含这个,但我在 Windows 上

【问题讨论】:

  • 不幸的是,标准 C++ 中没有完全跨平台的解决方案。我们需要知道您在哪个平台上。
  • 我忘了包括我在 Windows 上
  • 这能回答你的问题吗? C++ Win32 Console Color
  • 除非您启用支持,否则大多数可能的答案都不起作用,请参阅stackoverflow.com/q/16755142/103167

标签: c++


【解决方案1】:

我无法为您提供合适的 C++ 解决方案,但是 - C 库可以很好地与 C++ 配合使用,ncurses 可以成为您的朋友。引用this answer:

#include <curses.h>

int main() {
    initscr();
    start_color();
    init_pair(1, COLOR_BLACK, COLOR_RED);
    attron(COLOR_PAIR(1));
    printw("This should be printed in black with a red background!\n");
    refresh();
    getch(); // so the program doesn't just quit immediately
    endwin(); // not very RAII, is it? :-(
}

记得链接-lcurses(在类Unix机器上);如果你使用 CMake,或者在你的 CMakeLists.txt 中使用 find_package(Curses REQUIRED)

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 2013-06-27
    • 2016-03-04
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 2015-06-16
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多