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

xiaoxiao2021-03-06  77

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, which 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_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 I don't 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 procedure is as follows: # include // getstdhandle and setconsoletextttribute in header files Windows.h

#include

Using namespace std;

Void setColor (unsigned short backgroundColor = 0) // gives the parameter default value, allowing it to accept 0/1/2 parameter {handle hcon = getstdhandle (std_output_handle); // This example is output Take 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".

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

New Post(0)