使用串口终端软件比如 putty ,secureCRT等,是能够支持彩色的字体显示的。使用方法也很简单,先放代码:
#define COLOR_START "\033["
#define COLOR_STOP "\033[0m"
#define COLOR_TYPE_BOLD "1m"
#define COLOR_TYPE_UNDERLINE "4m"
#define COLOR_TYPE_BLINK "5m"
#define COLOR_TYPE_INVERSE "7m"
#define COLOR_TYPE_INVISIBLE "8m"
#define COLOR_TYPE_NONE "2m"
#define COLOR_BG_BLACK "40;"
#define COLOR_BG_RED "41;"
#define COLOR_BG_GREEN "42;"
#define COLOR_BG_BROWN "43;"
#define COLOR_BG_BLUE "44;"
#define COLOR_BG_PURPLE "45;"
#define COLOR_BG_BLUE2 "46;"
#define COLOR_BG_GREY "47;"
#define COLOR_FG_BLACK "30;"
#define COLOR_FG_RED "31;"
#define COLOR_FG_GREEN "32;"
#define COLOR_FG_YELLOW "33;"
#define COLOR_FG_BLUE "34;"
#define COLOR_FG_PURPLE "35;"
#define COLOR_FG_BLUE2 "36;"
#define COLOR_FG_WHITE "37;"
关键是格式,起始字符 "\033"+"[",这是开头,
"\033"是8进制,也可以写成16进制 "\x1b" , 效果一样,这是个特殊字符,终端不会显示出来。
接着是 背景色,前景色,特殊效果,以分号隔开,
比如红色字体,黑色背景,字体高亮是:
“31;40;1”
最后以m结尾。
完整的字符串是
"\033[31;40;1m"
终端收到这个字符串就会开始把以后收到的字符用黑底红字加粗显示。
要清除怎么办,发送字符串"\033[0m"就行了。
举个例子:
printf("\033[31;40;1m it's red \033[0m");或者用上面的宏
printf(COLOR_START COLOR_FG_RED COLOR_BG_BLACK COLOR_TYPE_BOLD "it's red" COLOR_STOP);
printf(COLOR_START COLOR_FG_GREEN COLOR_BG_BLACK COLOR_TYPE_BOLD "it's green" COLOR_STOP);
效果如下:
|