终端显示带有颜色的字符
终端显示带有颜色的字符
背景:
之前写过一个测试小工具就是测试读取OCR字符是否正确的工具,但是在显示窗口下只能显示白色字符,我希望是OCR正确识别的的显示绿色,错误识别的显示红色,这样一目了然,给老板做汇报的时候满屏幕花花绿绿的也显示自己水平高,后来找了半天资料引入了fmt库才实现显示。 今天在抖音刷到可以在printf函数设置显示颜色,然后做一下记录。
测试机器,win10系统, VS2022编写
字体设置不同的颜色
30 -37 用来显示字体的颜色 至于30到37显示那种颜色可以看截图,代码运行截图的
// 30 -37 用来显示字体的颜色
printf("显示字体色\n");
printf("\033[30m hello world\n");
printf("\033[31m hello world\n");
printf("\033[32m hello world\n");
printf("\033[33m hello world\n");
printf("\033[34m hello world\n");
printf("\033[35m hello world\n");
printf("\033[36m hello world\n");
printf("\033[37m hello world\n");
背景色
// 40 - 47 用来设置背景的颜色
printf("显示背景色\n");
printf("\033[40m hello world\n");
printf("\033[41m hello world\n");
printf("\033[42m hello world\n");
printf("\033[43m hello world\n");
printf("\033[44m hello world\n");
printf("\033[45m hello world\n");
printf("\033[46m hello world\n");
printf("\033[47m hello world\n");
光标移动 (这个用的估计不是很多)
// A 表示上移光标 B 表示下移光标 C表示右移光标 D表示左移光标 YXH设置光标的位置
printf("光标移动\n\n");
printf("\033[A Ahello world\n");
printf("\033[B Bhello world\n");
printf("\033[C Chello world\n");
printf("\033[D Dhello world\n");
字体设置
// 1m 是数字1 显示高亮, 如果不关闭下面打印的都高亮, 0m是关闭高亮 3m是斜体,4m是增加下划线5m让输出的内容闪烁, 7m反显示效果
printf("\033[1m hello world \033[0m\n");
printf(" hello world \n");
printf("\033[3m hello world\n");
printf("\033[5m hello world\n");
printf("\033[7m hello world\n");
动态显示
这个时间是可以刷新的,不太会接gif就不弄了。
while (1)
{
time_t cur = time(NULL);
struct tm* t = localtime(&cur);
printf("当前时间:%d: %d : %d\n", t->tm_hour, t->tm_min, t->tm_sec);
printf("\033[K"); // K 表示清空后面的内容
printf("\033[A"); // A表示向上移动一行,移动多行: 加上数字就行 \033[3A
_sleep(1000);
}
C++ cout 也可以
cout <<"显示字体色\n";
cout << "\033[30m hello world\n";
cout << "\033[31m hello world\n";
cout << "\033[32m hello world\n";
cout << "\033[33m hello world\n";
cout << "\033[34m hello world\n";
cout << "\033[35m hello world\n";
cout << "\033[36m hello world\n";
cout << "\033[37m hello world\n";
测试代码
#include <iostream>
#include <time.h>
#pragma warning(disable:4996) //关闭全部
using namespace std;
int main()
{
//while (1)
//{
//time_t cur = time(NULL);
//struct tm* t = localtime(&cur);
//printf("当前时间:%d: %d : %d\n", t->tm_hour, t->tm_min, t->tm_sec);
//printf("\033[K"); // K 表示清空后面的内容
//printf("\033[A"); // A表示向上移动一行,移动多行: 加上数字就行 \033[3A
//_sleep(1000);
// 1m 是数字1 显示高亮, 如果不关闭下面打印的都高亮, 0m是关闭高亮 3m是斜体,4m是增加下划线5m让输出的内容闪烁, 7m反显示效果
//printf("\033[1m hello world \033[0m\n");
//printf(" hello world \n");
//printf("\033[3m hello world\n");
//printf("\033[5m hello world\n");
//printf("\033[7m hello world\n");
// 30 -37 用来显示字体的颜色
//printf("显示字体色\n");
//printf("\033[30m hello world\n");
//printf("\033[31m hello world\n");
//printf("\033[32m hello world\n");
//printf("\033[33m hello world\n");
//printf("\033[34m hello world\n");
//printf("\033[35m hello world\n");
//printf("\033[36m hello world\n");
//printf("\033[37m hello world\n");
// 40 - 47 用来设置背景的颜色
//printf("显示背景色\n");
//printf("\033[40m hello world\n");
//printf("\033[41m hello world\n");
//printf("\033[42m hello world\n");
//printf("\033[43m hello world\n");
//printf("\033[44m hello world\n");
//printf("\033[45m hello world\n");
//printf("\033[46m hello world\n");
//printf("\033[47m hello world\n");
// A 表示上移光标 B 表示下移光标 C表示右移光标 D表示左移光标 YXH设置光标的位置
//printf("光标移动\n\n");
//printf("\033[A Ahello world\n");
//printf("\033[B Bhello world\n");
//printf("\033[C Chello world\n");
//printf("\033[D Dhello world\n");
//getchar();
//}
cout <<"显示字体色\n";
cout << "\033[30m hello world\n";
cout << "\033[31m hello world\n";
cout << "\033[32m hello world\n";
cout << "\033[33m hello world\n";
cout << "\033[34m hello world\n";
cout << "\033[35m hello world\n";
cout << "\033[36m hello world\n";
cout << "\033[37m hello world\n";
return 0;
}