Many times, we want to write a program can have a variety of colors can be some bright spots, although the system (color) function can set the color, but the system (color) color settings there is only one function, which is obviously not what we we want, what we need is a function called SetConsoleTextAttribute of.
SetConsoleTextAttribute () API function is a function of setting the background color and font color. Parameter table using two attributes (attributes used between spaced), different from the system (color), SetConsoleTextAttribute () can change the interface a plurality of colors, the system () can only be modified as a! .
head File:
<windows.h>
Prototype:
BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes);
parameter:
HANDLE hConsoleOutput
= GetStdHandle consolehwnd (STD_OUTPUT_HANDLE)
GetStdHandle (nStdHandle) is returned to the standard input, output or error handler device, that is, obtains a handle input / output error screen buffer.
Its parameter values nStdHandle following types of one of:
value | meaning |
---|---|
STD_INPUT_HANDLE | Standard input handle |
STD_OUTPUT_HANDLE | Handle standard output |
STD_ERROR_HANDLE | Standard error handle |
Here we only need to use STD_OUTPUT_HANDLE,
WORD wAttributesColor is used to set the parameters, there are three kinds of writing:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
//第二个参数填十六进制数字。
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
//直接填十进制数字。
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_BLACK | FOREGROUND_RED);
//BACKGROUND代表背景,FOREGROUND代表前景,
Here three kinds of written presentation:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x06);
printf("花狗\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x07);
printf("花狗\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x08);
printf("花狗\n");
The results are as follows:
Other color values:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10);
printf("花狗\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
printf("花狗\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14);
printf("花狗\n");
The results are as follows:
Other color data:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_GREEN | FOREGROUND_RED);
printf("花狗\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_GREEN | FOREGROUND_BLUE);
printf("花狗\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED | BACKGROUND_GREEN);
printf("花狗\n");
The results are as follows:
Other color values:
Attributes | meaning |
---|---|
FOREGROUND_BLUE | Text color contains blue |
FOREGROUND_GREEN | Text color contains green |
FOREGROUND_RED | Text color contains red |
FOREGROUND_INTENSITY | Text Color strengthen |
BACKGROUND_BLUE | Colors include blue background |
BACKGROUND_GREEN | Colors include green background |
BACKGROUND_RED | Colors include red background |
BACKGROUND_INTENSITY | The background color intensifies |
COMMON_LVB_LEADING_BYTE | Lead byte |
COMMON_LVB_TRAILING_BYTE | Trailing bytes |
COMMON_LVB_GRID_HORIZONTAL | Top level |
COMMON_LVB_GRID_LVERTICAL | Left Vertical |
COMMON_LVB_GRID_RVERTICAL | Correct vertical |
COMMON_LVB_REVERSE_VIDEO | Reverse the foreground and background properties |
COMMON_LVB_UNDERSCORE | Underline |
Well, other numerical own research we are interested in.
Each text sentence: Without ideal, achieving its purpose; not courageous, do not get something.