Http://blog.csdn.net/zwb8848happy/article/details/7284901
In VC, you need to call Windows API to change the font color.
The following is an example:
# Include <stdio. h>
# Include <windows. h>
Int main (void)
{
Printf ("Hello \ n ");
Setconsoletextattribute (getstdhandle (std_output_handle), foreground_green );
Printf ("Hello \ n ");
Setconsoletextattribute (getstdhandle (std_output_handle), foreground_blue | foreground_green | foreground_red );
Printf ("Hello \ n ");
Getchar ();
Return 0;
}
Among them, foreground has only four colors: green, blue, red, intensify (enhanced ).
Someone may ask, what if there are other colors?
This is simple. All colors are based on the three primary colors of red, green, and blue.
Here are two examples:
Red + Green = yellow green + Blue = cyan red + Blue = Magenta red + green + Blue = white
Yellow, blue, and magenta are both mixed with colors, so they are also called the secondary colors. In addition: Red + cyan = white green + Magenta = white blue + yellow = white 0. Set the number of output lines and columns on the consoleSystem ("Mode con: Cols = 100 lines = 20000 ");
1. Change the color of the entire Console
Use System ("color 0a ");
The value 0 after the color is the background color code, and the value a is the foreground color code. The color code is as follows:
0 = black
1 = blue
2 = green
3 = lake blue
4 = red
5 = purple
6 = yellow
7 = white
8 = gray
9 = light blue
A = light green
B = light green
C = light red
D = lavender
E = pale yellow
F = bright white
2. Change the color of the next output or input font and background.
Use the setconsoletextattribute function, as shown in figure
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 );
And so on.
Header files need to be introduced:
Windows. h
Function prototype:
Bool setconsoletextattribute (handle hconsoleoutput, word wattributes );
The value of wattributes corresponds to the following:
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.