c++ controls the color of font output

Keywords: Attribute Windows network

c++ controls the color of font output

_____

The default text color for the output box of a C++ console program is white, so I often refer to its output box as a black and white box.However, this text style is not fixed, it can change the color of the font.There are two methods:
1. Set the frame properties of the output box.Right-click on the title bar of the output box that pops up at run time to set its properties, which can adjust the size of the output box, background color, text color and other properties.However, this setting can only be run on your own machine.It's not useful on someone else's machine.There are no highlights here.
2. Modify with code.This is a description of the color of text displayed in C++ by modifying the output box with code.The code calls a function called SetConsoleTextAttribute (parameter table).
The SetConsoleTextAttribute() function is a function of the API to set font and background colors.Two attributes are used in the parameter table (separated by attributes).GetStdHandle() and FOREGROUND_or BACKGROUND_.* Value is INTENSITY or RED or GREEN or BLUE.The first property gets the handle (where you want to set the color), and the second property sets the color.Attribute addition is simply the addition of'|'between attribute values.
GetStdHandle(STD_OUTPUT_HANDLE) gets the handle.
FOREGROUND_INTENSITY indicates that the foreground color is set to highlight.
FOREGROUND_RED indicates that the foreground color is set to red, that is, the font color is red.
FOREGROUND_GREEN indicates that the foreground color is set to green, that is, the font color is green.
FOREGROUND_BLUE indicates that the foreground color is set to blue, that is, the font color is blue.
BACKGROUND_INTENSITY indicates setting the background color to highlight.
BACKGROUND_RED indicates setting the background color to red.
BACKGROUND_GREEN indicates that the background color is set to green.
BACKGROUND_BLUE indicates that the background color is set to blue.
...... ...... ...... ...... ...... ......
Note: Include window.h header file in preprocessing when using
Examples are as follows:
_____

#include <bits/stdc++.h>
#include<windows.h>
using namespace std;

int main()
{
	cout << "Primary color testCOLOR(No font color set)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
		FOREGROUND_GREEN | FOREGROUND_BLUE);//Set up tricolor addition
	cout << "white testCOLOR(Red Green Blue Addition)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
	//Set Red
	cout << "gules testCOLOR(Set the color to red)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN)
		;//Set Green
	cout << "green testCOLOR(Set the color to green)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
	//Set Blue
	cout << "blue testCOLOR(Set the color to blue)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
		FOREGROUND_GREEN);//Set red and green additions
	cout << "yellow testCOLOR(Red and green additive)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |
		FOREGROUND_BLUE);//Set red and blue additions
	cout << "Pink testCOLOR(Red and blue additive)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |
		FOREGROUND_BLUE);//Set the green and blue additions
	cout << "Cyan testCOLOR(Green and blue additive)" << endl;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY);
	//Set the color, no color added, so it is the primary color
	cout << endl;
	return 0;
}

Here I've made a header file for you, so you can use code that doesn't need to be typed that long in the future
_______

#include<windows.h>
void trun_red()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
}

void trun_blue()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
}

void trun_green()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}

void trun_yellow()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_GREEN);
}

void trun_pink()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED |FOREGROUND_BLUE);
}

void trun_white()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE |FOREGROUND_RED| FOREGROUND_GREEN);
}

void trun_cyan()
{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN |FOREGROUND_BLUE);
}

You can copy it and make it into a header file. Note:
Save with'.h'suffix name, not'.cpp'.
No kid can download it on the network!
Disk address: https://pan.baidu.com/s/1EzYpx6gC-RFqfCrH63Y79g

Posted by danoli on Fri, 16 Aug 2019 23:07:18 -0700