Use VC ++ to write the Console Program All day, black paper white screen, feel very depressed? Many people want to use some functions in conio. h/graphics. h, but they find that VC ++ does not have these header files. Of course, conio. h/graphics. H is the proprietary header file of Borland TC/BC, so these files are not found in VC ++. Copy the two header files and then use ?? Of course, the answer is no. In fact, VC ++ also has related functions to display the console color text and background. Next we will look at how VC ++ implements color text.
Use the API functions getstdhandle () and setconsoletextattribute () to implement color background and color text. The following describes the meanings of the two function Declarations and their parameters. First, getstdhandle () is declared as follows:
Handle getstdhandle (DWORD nstdhandle );
Getstdhandle () returns the handle of the standard input, output, or wrong device, that is, the handle to the screen buffer that gets the input, output, or error. The value of nstdhandle is of the following types:
Value Meaning
Std_input_handle standard input handle
Std_output_handle standard output handle
Handle With std_error_handle standard error
the setconsoletextattribute () function is used to set the text color and background color of the input or output text in the console. Color text can only be displayed after this function is set. The function prototype is:
bool setconsoletextattribute (
handle hconsoleoutput, // handle of console screen buffer
word wattributes // color of text and background
);
If the function successfully sets the text and background color, the return value is non-zero. If the function fails, the return value is zero. The parameter meanings are as follows:
Hconsoleoutput ------------- handle of the console screen buffer.
Word wattributes ----------- text and background color.
The text and background colors can be foreground_blue, foreground_green, foreground_red, foreground_intensity, background_blue, background_green, background_red, and background_intensity. I don't know how to combine it with any color combined with these colors (which is a bit similar to RGB.
Next, we will write a function setcolor () similar to settextcolor in TC, but the following function can set the background color at the same time. The function implementation is as follows:
Void setcolor (unsigned short forecolor, unsigned short backgroundcolor)
{
Handle hcon = getstdhandle (std_output_handle );
// Obtain the buffer handle
Setconsoletextattribute (hcon, forecolor backgroundcolor );
// Set the text and background color
};
With the above functions, we can implement the color text and background, and give our own programs a distinctive output and prompt. Next we will implement a colorful Hello world !. The procedure is as follows:
# Include
// Getstdhandle and setconsoletextattribute are in the header file windows. h.
# Include
Using namespace STD;
Void setcolor (unsigned short forecolor = 4, unsigned short backgroundcolor = 0)
// Give the parameter default value to make it
// Accept 0/1/2 parameters
{
Handle hcon = getstdhandle (std_output_handle); // This example uses the output as an example.
Setconsoletextattribute (hcon, forecolor backgroundcolor );
};
Int main ()
{
Setcolor ();
STD: cout <"Hello world! "<Setcolor (40, 30 );
STD: cout <"Hello world! "<Return 0;
}
Such a colorful Hello world! The program has been written.
Of course, you can use this function to display some special output or prompt information in the appropriate place of your program, so that your program will be "different.