Implement the console program with VC ++ to display color text

xiaoxiao2021-03-06  53

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 setconsoletextAttribute (). Let's talk about the meaning of these two functions declarations and their parameters. First, getStdHandle (), which declares the 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 its parameter nstdhandle is one of the following types: Value Meaning STD_INPUT_HANDLE standard input handle STD_OUTPUT_HANDLE standard output handle STD_ERROR_HANDLE standard error handle

The function setconsoletextAttribute () is the text color and background color of the input or output text in the Console program. 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); CONSOLETEXTATTATTRIBUTE;

If the function sets the text and the background color, it returns non-zero; if the failure is returned 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 function is implemented as follows: Void setColor (unsigned short backgroundcolor) {handle hcon = GetStdHandle; // Get buffer handle setconsoletexttattribute (hcon, forecolor | backgroundcolor); // Set text and background color}; With the above functions We can implement color text and background, give your own program with Different outputs and prompts. Let's take a colored Hello World! . The procedures are as follows: #include // getstdhandle and setconsoletextAttribute in header file Windows.h #include using namespace std;

Void setColor (unsigned short forecolor = 3, unsigned short backgroundcolor = 0) / / The default value for the parameter, allowing it to accept 0/1/2 parameters {handle hcon = getstdhandle (std_output_handle); // This example is output Take SETCONSOLETEXTATTRIBUTE (HCON, Forecolor | BackgroundColor);

INT main () {setColor (); std :: cout << "Hello World!" << Endl; setColor (40, 30); std :: cout << "Hello World!" << endl; 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".

转载请注明原文地址:https://www.9cbs.com/read-116895.html

New Post(0)