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 it fails 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! . Procedures are as follows: #include // GetStdHandle and SetConsoleTextAttribute windows.h header file in #include using namespace std; void SetColor (unsigned short ForeColor = 4, unsigned short BackGroundColor = 0) // default value to the parameter, it can be // Accept 0/1/2 parameter {handle hcon = getstdhandle (std_output_handle); // This example is used as an example setconsoletextttribute (hcon, forecolor backgroundcolor);}; int main () {setColor (); std :: cout << "Hello World!"