C++ 设置控制台输出颜色

时间:2020-12-31 23:29:04
#include <stdint.h>
#include <iostream>
#include <string>
#include <Windows.h> // 0 = 黑色 8 = 灰色
// 1 = 蓝色 9 = 淡蓝色
// 2 = 绿色 A = 淡绿色
// 3 = 浅绿色 B = 淡浅绿色
// 4 = 红色 C = 淡红色
// 5 = 紫色 D = 淡紫色
// 6 = 黄色 E = 淡黄色
// 7 = 白色 F = 亮白色 // 控制台前景颜色
enum ConsoleForegroundColor
{
enmCFC_Red = FOREGROUND_INTENSITY | FOREGROUND_RED,
enmCFC_Green = FOREGROUND_INTENSITY | FOREGROUND_GREEN,
enmCFC_Blue = FOREGROUND_INTENSITY | FOREGROUND_BLUE,
enmCFC_Yellow = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
enmCFC_Purple = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
enmCFC_Cyan = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
enmCFC_Gray = FOREGROUND_INTENSITY,
enmCFC_White = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
enmCFC_HighWhite = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
enmCFC_Black = ,
}; enum ConsoleBackGroundColor
{
enmCBC_Red = BACKGROUND_INTENSITY | BACKGROUND_RED,
enmCBC_Green = BACKGROUND_INTENSITY | BACKGROUND_GREEN,
enmCBC_Blue = BACKGROUND_INTENSITY | BACKGROUND_BLUE,
enmCBC_Yellow = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
enmCBC_Purple = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
enmCBC_Cyan = BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
enmCBC_White = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
enmCBC_HighWhite = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
enmCBC_Black = ,
}; void SetConsoleColor(ConsoleForegroundColor foreColor = enmCFC_White, ConsoleBackGroundColor backColor = enmCBC_Black)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, foreColor | backColor);
} int32_t main()
{
int32_t allForeColors[] = { enmCFC_Red, enmCFC_Green, enmCFC_Blue, enmCFC_Yellow, enmCFC_Purple, enmCFC_Cyan, enmCFC_Gray, enmCFC_White, enmCFC_HighWhite, enmCFC_Black };
std::string allForeColorsName[] = { "enmCFC_Red", "enmCFC_Green", "enmCFC_Blue", "enmCFC_Yellow", "enmCFC_Purple", "enmCFC_Cyan", "enmCFC_Gray", "enmCFC_White", "enmCFC_HighWhite", "enmCFC_Black" };
int32_t allBackColors[] = { enmCBC_Red, enmCBC_Green, enmCBC_Blue, enmCBC_Yellow, enmCBC_Purple, enmCBC_Cyan, , enmCBC_White, enmCBC_HighWhite, enmCBC_Black };
std::string allBackColorsName[] = { "enmCBC_Red", "enmCBC_Green", "enmCBC_Blue", "enmCBC_Yellow", "enmCBC_Purple", "enmCBC_Cyan", "", "enmCBC_White", "enmCBC_HighWhite", "enmCBC_Black " };
for(uint32_t i = ; i < sizeof(allForeColors) / sizeof(allForeColors[]); ++i)
{
for(uint32_t j = ; j < sizeof(allForeColors) / sizeof(allForeColors[]); ++j)
{
if(i != j && allBackColorsName[j] != "")
{
SetConsoleColor((ConsoleForegroundColor)allForeColors[i], (ConsoleBackGroundColor)allBackColors[j]);
std::cout << "ForeColor:" << allForeColorsName[i] <<",backColor:"<< allBackColorsName[j] << std::endl;
}
}
}
getchar();
return ;
}

C++ 设置控制台输出颜色