Implement the console program with VC to display color text
When writing the console program with VC , the screen of the black paper is very depressed throughout the day. Many people want to implement some of the functions in Conio.h / Graphics.h, but found that VC does not have these headers. Of course, conio.h / graphics.h is the Borland TC / BC proprietary header file, so there is no such files at all in VC . Come all the two header files, then use? ? The answer is of course negative. In fact, VC also has related functions to implement console color text and background display. Let's take a look at how VC implements color text. Realize colorful backgrounds and color text in VC getStdHandle () and setConsoleTextAnda () and setconsoletextAttribute (). Let's talk about the meaning of these two functions declarations and their parameters. First of all, getstdhandle (), which is as follows
Handle getstdhandle (DWORD NSTDHANDLE); getStdHandle () Returns the handle of the standard input, output, or erroneous device, that is, the handle of the input, output / error screen buffer. The value of the parameter nstdhandle is one of the following types: Value STD_INPUT_HANDLE standard input handle STD_OUTPUT_HANDLE standard output handle STD_ERROR_HANDLETATTRIBUTE () role is the text color of the Console program setting or output text and output text. background color. Only the colorful text can only be displayed after this function is set. Its function prototype is: BOOL SetConsoleTextAttribute (Handle Hconsoleoutput, handle Word Wattributes // text and background color); if the function sets text and background color success, return non-zero; if the failure returns zero. Its parameters are as follows: hconsoleoutput ------------- The handle of the Console screen buffer. Word Wattributes ----------- The color of the text and the 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 combination of these types of color into (this is somewhat similar to the RGB) As to how the portfolio, I do not know. Below, we will write a function setColor () similar to the SETTEXTCOLOR similar to TC, but the following function can set the background color at the same time, the implementation of the function is as follows:
void SetColor (unsigned short ForeColor, unsigned short BackGroundColor) {HANDLE hCon = GetStdHandle (STD_OUTPUT_HANDLE); // get buffer handle SetConsoleTextAttribute (hCon, ForeColor BackGroundColor); // set the color of the text and background}; With the above function we You can implement color text and background, give your own program, a different output and prompt. Let's take a colored Hello World! . The procedure is as follows: #include
// getstdhandle and setconsoletextAtttribute in header files Windows.h
#include
Using namespace std;
Void SetColor (unsigned short forecolor = 4, unsigned short backgroundcolor = 0)
/ / The default value for the parameter, make it
/ / Accept 0/1/2 parameters
{
Handle hcon = getstdhandle (std_output_handle); // This example is an example
SetConSoleTextAttribute (HCON, Forecolor BackgroundColor);
}
int main ()
{
SetColor ();
Std :: cout << "Hello World!" <
SetColor (40, 30);
Std :: cout << "Hello World!" <
Return 0;
}
Such a colored Hello World! Program is written. Of course, you can use this function to show some special output or prompt information in your proper place, so your program is "different".