Windows命令行下实现带颜色输出

改变下一个输出或者输入字体和背景的颜色 采用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.


你可能感兴趣的:(C语言,命令行,颜色)