本文章在Linux进行演示!!!
使用C语言输出不同颜色字体和背景
格式:
printf("\033[字体背景颜色;字体颜色m字符串\033[0m");
上边的 \033 也可以用 \e 来代替。
字体颜色与字符的对应关系
| 字符 | 颜色 |
| 30 | 黑色 |
| 31 | 红色 |
| 32 | 绿色 |
| 33 | 黄色 |
| 34 | 蓝色 |
| 35 | 紫色 |
| 36 | 深绿色 |
| 37 | 白色 |
字体背景颜色与字符的对应关系
| 字符 | 背景颜色 |
| 40 | 黑色 |
| 41 | 红色 |
| 42 | 绿色 |
| 43 | 黄色 |
| 44 | 蓝色 |
| 45 | 紫色 |
| 46 | 深绿色 |
| 47 | 白色 |
屏幕控制与字符的对应关系
| 字符 | 动作 |
| 0 | 关闭所有属性 |
| 1 | 设置高亮 |
| 4 | 下划线 |
| 5 | 闪烁 |
| 7 | 反显 |
| 8 | 消隐 |
简易版的彩色进度条
#include <iostream>
#include <string>
#include <string.h>
#include <unistd.h>
using namespace std;
int main()
{
char buf[102];
memset(buf,0,sizeof(buf));
int cnt=0;
string s="|/-\\";
while(cnt<=100)
{
switch (cnt%8)
{
case 0:
printf("\033[40;31m[%-100s][%d%%]%c\r\e[0m",buf,cnt,s[cnt%4]);
break;
case 1:
printf("\033[40;32m[%-100s][%d%%]%c\r\e[0m",buf,cnt,s[cnt%4]);
break;
case 2:
printf("\033[40;33m[%-100s][%d%%]%c\r\e[0m",buf,cnt,s[cnt%4]);
break;
case 3:
printf("\033[40;34m[%-100s][%d%%]%c\r\e[0m",buf,cnt,s[cnt%4]);
break;
case 4:
printf("\033[40;35m[%-100s][%d%%]%c\r\e[0m",buf,cnt,s[cnt%4]);
break;
case 5:
printf("\033[40;36m[%-100s][%d%%]%c\r\e[0m",buf,cnt,s[cnt%4]);
break;
case 6:
printf("\033[40;37m[%-100s][%d%%]%c\r\e[0m",buf,cnt,s[cnt%4]);
break;
default:
break;
}
buf[cnt++]='#';
fflush(stdout);
usleep(10000);
}
cout<<endl;
return 0;
}
部分输出截图



















