1. 程式人生 > >c++ system(“color XX”)具體顏色及其用法;

c++ system(“color XX”)具體顏色及其用法;

1.改變整個控制檯的顏色


用 system("color 0A"); 


其中color後面的0是背景色代號,A是前景色代號。各顏色程式碼如下: 


0=黑色 


1=藍色 


2=綠色 


3=湖藍色 


4=紅色 


5=紫色 


6=黃色 


7=白色 


8=灰色 


9=淡藍色 


A=淡綠色 


B=淡淺綠色 


C=淡紅色 


D=淡紫色 


E=淡黃色 


F=亮白色 
void account::entrance()
{
	system("color 0b");//!
    char n;
    machine m;
	while(1)
	{
    system("cls");
    cout <<endl;
	cout<<"\t\t  O(∩_∩)O~~ 歡迎進入南陽康佳公司的裝置管理系統\n\n";
    cout<<"\t\t╔*☆***◎***◇***☆*** 選單 ***☆***◇***◎***☆*╗\n";
    cout<<"\t\t|★|\t\t               \t\t\t|★|\n";
	cout<<"\t\t|☆|\t\t               \t\t\t|☆|\n";
	cout<<"\t\t|★|\t\t 1.裝置資訊顯示\t\t\t|★|\n";
    cout<<"\t\t|☆|\t\t 2.裝置資訊查詢\t\t\t|☆|\n";
	cout<<"\t\t|★|\t\t 3.返回        \t\t\t|★|\n";
	cout<<"\t\t|☆|\t\t               \t\t\t|☆|\n";
	cout<<"\t\t|★|\t\t               \t\t\t|★|\n";
	cout<<"\t\t|☆|\t\t               \t\t\t|☆|\n";
	cout<<"\t\t╚*☆***◎***◇****☆****★****☆****◇***◎***☆*╝\n\n";
    cout<<"\t\t請選擇您要實現的功能選項 (1-3): [ ]\b\b";
	while(1)
	{
		cin>>n;getchar();
		if(n<'1'||n>'3')
			printf("輸入錯誤,請您重新輸入選項(1-3):[ ]\b\b");
		else
			break;
	}
	if(n=='1') {m.show();}
	if(n=='2') m.select();
	if(n=='3') return;
	}
}



2.改變下一個輸出或者輸入字型和背景的顏色


採用SetConsoleTextAttribute函式,如


White on Black:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |


            FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);


Red on Black:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |


            FOREGROUND_RED);


Green on Black:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |


            FOREGROUND_GREEN);


Yellow on Black:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |


            FOREGROUND_RED | FOREGROUND_GREEN);


Blue on Black:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |


            FOREGROUND_BLUE);


Magenta on Black:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |


            FOREGROUND_RED | FOREGROUND_BLUE);


Cyan on Black:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |


            FOREGROUND_GREEN | FOREGROUND_BLUE);


Black on Gray:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |


            BACKGROUND_INTENSITY);


Black on White:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |


            FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);


Red on White:


        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |


            FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |


            FOREGROUND_RED);


等等。


需要引入標頭檔案:


windows.h


函式原型:


BOOL SetConsoleTextAttribute( HANDLE hConsoleOutput, WORD wAttributes ); 


wAttributes 的取值含義對應如下:


Attribute Meaning 


FOREGROUND_BLUE Text color contains blue. 


FOREGROUND_GREEN Text color contains green. 


FOREGROUND_RED Text color contains red. 


FOREGROUND_INTENSITY Text color is intensified. 


BACKGROUND_BLUE Background color contains blue. 


BACKGROUND_GREEN Background color contains green. 


BACKGROUND_RED Background color contains red. 


BACKGROUND_INTENSITY Background color is intensified. 


COMMON_LVB_LEADING_BYTE Leading byte. 


COMMON_LVB_TRAILING_BYTE Trailing byte. 


COMMON_LVB_GRID_HORIZONTAL Top horizontal. 


COMMON_LVB_GRID_LVERTICAL Left vertical. 


COMMON_LVB_GRID_RVERTICAL Right vertical. 


COMMON_LVB_REVERSE_VIDEO Reverse foreground and background attributes. 


COMMON_LVB_UNDERSCORE Underscore.