C++ 設(shè)置控制臺(tái)(命令行)窗口 光標(biāo)位置,及前背景顏色
核心代碼
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
/*
#define FOREGROUND_BLUE 0x0001 // text color contains blue.
#define FOREGROUND_GREEN 0x0002 // text color contains green.
#define FOREGROUND_RED 0x0004 // text color contains red.
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
#define BACKGROUND_BLUE 0x0010 // background color contains blue.
#define BACKGROUND_GREEN 0x0020 // background color contains green.
#define BACKGROUND_RED 0x0040 // background color contains red.
#define BACKGROUND_INTENSITY 0x0080 // background color is intensified.
*/
//更改當(dāng)前輸出的顏色(前景色/背景色)
void ColorPrintf(WORD cl,char* str)
{
static HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
//WORD wOldColorAttrs;
//CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
//First save the current color information
//GetConsoleScreenBufferInfo(h, &csbiInfo);
//wOldColorAttrs = csbiInfo.wAttributes;
//Set the new color information
SetConsoleTextAttribute ( h, cl );
printf ( str);
//Restore the original colors
//SetConsoleTextAttribute ( h, wOldColorAttrs);
SetConsoleTextAttribute(h, FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);
}
//移動(dòng)輸入光標(biāo)位置
void MoveCursorTo(int x,int y)
{
static HANDLE m=GetStdHandle(STD_OUTPUT_HANDLE);
COORD cp={x,y};
SetConsoleCursorPosition(m,cp);
}
int main ( void )
{
char st[10];
ColorPrintf (FOREGROUND_BLUE | FOREGROUND_INTENSITY, "This is a color test\n" );
for (int j=0;j<255;j+=16)
{
for (int i=0;i<16;i++)
{
sprintf(st,"%02x ",j+i);
ColorPrintf(j+i,st);
}
printf("\n");
}
//printf("\n\n");
//MoveCursorTo( 1, 9 );
//ColorPrintf(0x0083,"This is a test\n");
return 0;
}
終端/控制臺(tái)設(shè)置顏色字體、光標(biāo)定位和清屏
printf("\033[47;31mhello world\033[5m");
47是字背景顏色, 31是字體的顏色, hello world是字符串. 后面的\033[5m是控制碼.
顏色代碼:
QUOTE:
字背景顏色范圍: 40--49 字顏色: 30--39
40: 黑 30: 黑
41: 紅 31: 紅
42: 綠 32: 綠
43: 黃 33: 黃
44: 藍(lán) 34: 藍(lán)
45: 紫 35: 紫
46: 深綠 36: 深綠
47: 白色 37: 白色
ANSI控制碼:
QUOTE:
\033[0m 關(guān)閉所有屬性
\033[1m 設(shè)置高亮度
\03[4m 下劃線
\033[5m 閃爍
\033[7m 反顯
\033[8m 消隱
\033[30m -- \033[37m 設(shè)置前景色
\033[40m -- \033[47m 設(shè)置背景色
\033[nA 光標(biāo)上移n行
\03[nB 光標(biāo)下移n行
\033[nC 光標(biāo)右移n行
\033[nD 光標(biāo)左移n行
\033[y;xH設(shè)置光標(biāo)位置
\033[2J 清屏
\033[K 清除從光標(biāo)到行尾的內(nèi)容
\033[s 保存光標(biāo)位置
\033[u 恢復(fù)光標(biāo)位置
\033[?25l 隱藏光標(biāo)
\33[?25h 顯示光標(biāo)
這樣, 在某些時(shí)候就可以實(shí)現(xiàn)動(dòng)態(tài)的輸出.
相關(guān)文章
C++?AVL樹的兩單旋和兩雙旋的項(xiàng)目實(shí)踐
本文主要介紹了C++?AVL樹的兩單旋和兩雙旋的項(xiàng)目實(shí)踐,根據(jù)節(jié)點(diǎn)插入位置的不同,AVL樹的旋轉(zhuǎn)分為四種,下面就來介紹一下,感興趣的可以了解一下2024-03-03
C++使用map實(shí)現(xiàn)多進(jìn)程拷貝文件的程序思路
這篇文章主要介紹了C++使用mmap實(shí)現(xiàn)多進(jìn)程拷貝文件,通過本文給大家分享程序思路及完整代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
C語言中的pause()函數(shù)和alarm()函數(shù)以及sleep()函數(shù)
這篇文章主要介紹了C語言中的pause()函數(shù)和alarm()函數(shù)以及sleep()函數(shù),是C語言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C/C++ 中const關(guān)鍵字的用法小結(jié)
C++中的const關(guān)鍵字的用法非常靈活,而使用const將大大改善程序的健壯性。這篇文章主要介紹了C/C++ 中const關(guān)鍵字的用法,需要的朋友可以參考下2020-02-02

