2012-07-20 41 views

回答

1

您可以使用VT100 escape codes为文本输出添加颜色。那么你不需要任何外部库或程序。

例子:

printf("This text is \x1b[31mred \x1b[32mgreen \x1b[0mnormal.\n"); 

编辑:逃生代码不会在Windows命令提示符下运行,而不是必须使用这样的功能SetConsoleTextAttribute

这里是展示了如何使用它的一个例子:

#include <windows.h> 
#include <stdio.h> 

int main() 
{ 
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 

    CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo; 
    GetConsoleScreenBufferInfo(hConsole, &ConsoleInfo); 
    int originalAttrs = ConsoleInfo.wAttributes; 

    printf("This text is "); 
    SetConsoleTextAttribute(hConsole, FOREGROUND_RED); 
    printf("red "); 
    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN); 
    printf("green "); 
    SetConsoleTextAttribute(hConsole, originalAttrs); 
    printf("normal\n"); 

    return 0; 
} 
+0

预包装库不幸的是,它会显示它的字面.. – latenightcode 2012-07-20 08:18:44

+0

@vincentbelkin编辑我的回答对一个特定的Windows的解决方案。它的功能在Windows中是标准的,所以仍然不需要外部库或程序。 – 2012-07-20 08:43:25

+0

谢谢你!要学会如何使用这 – latenightcode 2012-07-20 08:53:29

0

当然有。 libcaca是一个广泛支持的基于文本的图形库,也许你会发现它很有用。

+0

没有我的意思是,在GCC – latenightcode 2012-07-20 07:12:21