有一种方法可以在C中改变输出颜色吗?例如:
#include <stdio.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
int main(int argc, char const *argv[])
{
printf(ANSI_COLOR_RED "This text is RED!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_GREEN "This text is GREEN!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_BLUE "This text is BLUE!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_CYAN "This text is CYAN!" ANSI_COLOR_RESET "\n");
return 0;
}我测试了这个代码,但是颜色刚刚在vs代码终端显示出来。我想知道当我编译从代码生成的二进制代码时,是否有一种方法也可以在cmd中显示出来。
发布于 2021-09-23 14:37:23
简而言之,答案是肯定的。
但它与C无关,它取决于您使用的操作系统和环境,如控制台的类型。在Windows下,控制台模式在Microsoft Docs上有完整的文档。
在Linux下,您可以使用基于Digital VT终端系列命令代码的90年代的转义代码。这些命令在Windows的最新版本中也是可用的,就像VT-Terminals一样,所以你现在可以在Linux Windows或MacOS上使用一些可移植的东西。
这是Linux下常用的curses库,最初是从Unix System V,现在以ncurses的形式,也移植到Windows上,在屏幕和颜色的控制上有一些帮助
https://stackoverflow.com/questions/69301292
复制相似问题