C\C++ 终端输出带有颜色的字符

终端显示带有颜色的字符

背景:

之前写过一个测试小工具就是测试读取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;
}



准确的说这个能力是shell窗口提供的,跟语言没有关系,一般输出到终端的都可以用这个方法。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

波雅_汉库克

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值