How to change the color of text in C++ (different words display different colors)

Many students only have black and white colors when making c++ games. It's like a si person. It affects the visual effect very much and looks very ugly. Therefore, I decided to post an article that changes the color of the text!
insert image description here

The following introduces the method:
before understanding the program, first of all, it is better to understand the result of mixing the three primary colors of light and the three primary colors.
The three primary colors of light:

  • red
  • blue
  • Green
    Then we can observe their mixed effect:
    insert image description here
    then we just need to use them in the program!
    In fact, it is very simple, but the code is a bit long, the following is the function:
void color(int x)
{
    
    
	switch(x)
	  {
    
    
	    case  1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_RED  );break;	//红
	  	case  2:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_BLUE );break;	//蓝
	    case  3:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN);break;	//绿
		case  4:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_RED  |FOREGROUND_BLUE );break;	//红+蓝=紫
		case  5:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_RED  |FOREGROUND_GREEN);break;	//红+绿=黄
		case  6:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_BLUE |FOREGROUND_GREEN);break;	//蓝+绿=青
	    case  7:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN|FOREGROUND_BLUE |FOREGROUND_RED);break;	//红+蓝+绿=白
	    default:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN|FOREGROUND_BLUE |FOREGROUND_RED);break;
	  }
}

The usage of this function is as follows:

#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
void color(int x)
{
    
    
	switch(x)
	  {
    
    
	    case  1:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_RED  );break;
	  	case  2:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_BLUE );break;
	    case  3:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN);break;
		case  4:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_RED  |FOREGROUND_BLUE );break;
		case  5:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_RED  |FOREGROUND_GREEN);break;
		case  6:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_BLUE |FOREGROUND_GREEN);break;
	    case  7:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN|FOREGROUND_BLUE |FOREGROUND_RED);break;
	    default:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY |FOREGROUND_GREEN|FOREGROUND_BLUE |FOREGROUND_RED);break;
	  }
}
int main()
{
    
    
	int a=1;
	system ("color 0f");
	while (a<=7)
	{
    
    
		color (a);
		
		//调用函数,函数后面所有的输出都是这个颜色,除非重新调用函数改变颜色 
		
		cout <<a <<" :当前数字对应的颜色  ABC 123  /;'[]!,.`~@#$--&^%&=-`'\n";
		a++;
	}
}

Guess you like

Origin blog.csdn.net/DUXS11/article/details/132142653