終端顯示帶有顏色的字符
終端顯示帶有顏色的字符
- 終端顯示帶有顏色的字符
- 背景:
- 測試機器,win10系統, VS2022編寫
- 字體設置不同的顏色
- 背景色
- 光標移動 (這個用的估計不是很多)
- 字體設置
- 動態顯示
- C++ cout 也可以
- 測試代碼
- 準確的說這個能力是shell窗口提供的,跟語言沒有關系,一般輸出到終端的都可以用這個方法。
- 擴展
背景:
之前寫過一個測試小工具就是測試讀取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窗口提供的,跟語言沒有關系,一般輸出到終端的都可以用這個方法。
擴展
這位老哥寫的比我清楚