# include & ltwindows.h & gt
# include & ltiostream & gt
Use namespace std
void set color(unsigned short ForeColor = 0,unsigned short BackGroundColor=0)
{
HANDLE hCon = GetStdHandle(STD _ OUTPUT _ HANDLE);
SetConsoleTextAttribute(hCon,ForeColor | background color);
}
int main()
{
int FC,BC;
for(BC = 0; BC & lt= 17; BC++)
{
for(FC = 0; FC & lt=255; FC++)
{
SetColor(FC,BC);
STD::cout & lt; & ltFC & lt& lt,' & lt& ltBC & lt& lt\ t ';
SetColor(7,0);
if(FC % 16 = = 15)STD::cout & lt; & ltendl
}
}
Returns 0;
}
Dazzling, right? Simply put:
Using API functions GetStdHandle () and SetConsoleTextAttribute () to realize color background and color text in vc++.
Let's talk about the meaning of these two function declarations and their parameters. First of all, let's talk about GetStdHandle (), which is declared as follows
Handle GetStdHandle (
Gandel, Dword Ernst
);
GetStdHandle () returns the handle of the standard input, output or error device, that is, the input, output/error screen is obtained.
Handle to the buffer. The value of its parameter nStdHandle is one of the following types:
Value meaning
Handle of standard input.
Handle to standard output.
Handle to the standard error.
The SetConsoleTextAttribute () function is used to set the text color and background color of the input or output text in the console program.
Color. Only when this function is set can colored text be displayed. Its functional prototype is:
BOOL SetConsoleTextAttribute(
Handle HConsoleOutput,//The handle to the console screen buffer.
Word attribute//Color of text and background
);
If the function successfully sets the text and background colors, it returns a non-zero value; If it fails, it returns zero. Its parameters are as follows:
Handle to the console screen buffer.
Text properties-color of text and background.
Its text and background color can be foreground blue, foreground green, foreground red, foreground intensity,
Background _ blue, background _ green, background _ red and background _ intensity.
And any color that these colors combine into, the dots are a bit similar to RGB, but how to combine them is not clear. ...