控制台输出彩色文字
1.Unbuntu系统
#define RESET "\033[0m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
#define BOLDBLACK "\033[1m\033[30m"
#define BOLDRED "\033[1m\033[31m"
#define BOLDGREEN "\033[1m\033[32m"
#define BOLDYELLOW "\033[1m\033[33m"
#define BOLDBLUE "\033[1m\033[34m"
#define BOLDMAGENTA "\033[1m\033[35m"
#define BOLDCYAN "\033[1m\033[36m"
#define BOLDWHITE "\033[1m\033[37m"
实际输出时,根据颜色可直接输出
std::cout << "\033[31m]" << "Red words" << "\033[0m" << std::endl;
inline std::string GetConsoleFrontColor(CONSOLE_FRONT_COLOR_E cfc = CONSOLE_FRONT_COLOR_E::CFC_WHITE)
{
std::string cfc_color = WHITE;
switch (cfc)
{
case CONSOLE_FRONT_COLOR_E::CFC_RESET:
cfc_color = RESET;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BLACK:
cfc_color = BLACK;
break;
case CONSOLE_FRONT_COLOR_E::CFC_RED:
cfc_color = RED;
break;
case CONSOLE_FRONT_COLOR_E::CFC_GREEN:
cfc_color = GREEN;
break;
case CONSOLE_FRONT_COLOR_E::CFC_YELLOW:
cfc_color = YELLOW;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BLUE:
cfc_color = BLUE;
break;
case CONSOLE_FRONT_COLOR_E::CFC_MAGENTA:
cfc_color = MAGENTA;
break;
case CONSOLE_FRONT_COLOR_E::CFC_CYAN:
cfc_color = CYAN;
break;
case CONSOLE_FRONT_COLOR_E::CFC_WHITE:
cfc_color = WHITE;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDBLACK:
cfc_color = BOLDBLACK;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDRED:
cfc_color = BOLDRED;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDGREEN:
cfc_color = BOLDGREEN;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDYELLOW:
cfc_color = BOLDYELLOW;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDBLUE:
cfc_color = BOLDBLUE;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDMAGENTA:
cfc_color = BOLDMAGENTA;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDCYAN:
cfc_color = BOLDCYAN;
break;
case CONSOLE_FRONT_COLOR_E::CFC_BOLDWHITE:
cfc_color = BOLDWHITE;
break;
default:
cfc_color = WHITE;
break;
}
return cfc_color;
};
-
函数调用 void ShowMessage(const std::string msg, CONSOLE_FRONT_COLOR_E cfc)
{
std::string color = GetConsoleFrontColor(cfc);
std::string color_reset = GetConsoleFrontColor(CONSOLE_FRONT_COLOR_E::CFC_RESET);
std::cout << color << msg << color_reset << std::endl;
}
ShowMessage("Hello world!", CONSOLE_FRONT_COLOR_E::CFC_GREEN);
参考:
(原+转)ubuntu终端输出彩色文字
2.Windows控制台颜色
windows采用16进制数表示各个颜色,颜色之间可以组合
#include <stdint.h>
#include <iostream>
#include <string>
#include <Windows.h>
enum ConsoleForegroundColor
{
CFC_Red = FOREGROUND_RED,
CFC_RedI = FOREGROUND_INTENSITY | FOREGROUND_RED,
CFC_Green = FOREGROUND_INTENSITY | FOREGROUND_GREEN,
CFC_Blue = FOREGROUND_INTENSITY | FOREGROUND_BLUE,
CFC_Yellow = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
CFC_Purple = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
CFC_Cyan = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
CFC_Gray = FOREGROUND_INTENSITY,
CFC_White = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
CFC_HighWhite = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
CFC_Black = 0,
};
enum ConsoleBackGroundColor
{
CBC_Red = BACKGROUND_INTENSITY | BACKGROUND_RED,
CBC_Green = BACKGROUND_INTENSITY | BACKGROUND_GREEN,
CBC_Blue = BACKGROUND_INTENSITY | BACKGROUND_BLUE,
CBC_Yellow = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN,
CBC_Purple = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE,
CBC_Cyan = BACKGROUND_INTENSITY | BACKGROUND_GREEN | BACKGROUND_BLUE,
CBC_White = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
CBC_HighWhite = BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE,
CBC_Black = 0,
};
void SetConsoleColor(ConsoleForegroundColor foreColor = CFC_White, ConsoleBackGroundColor backColor = CBC_Black)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, foreColor | backColor);
}
void ShowMessage(std::string msg, ConsoleForegroundColor foreColor = CFC_White, ConsoleBackGroundColor backColor = CBC_Black)
{
SetConsoleColor(foreColor, backColor);
std::cout << msg << std::endl;
SetConsoleColor(CFC_White, CBC_Black);
}
int main(int argc, char** argv)
{
ShowMessage("Hello World QCJ.", CFC_Red, CBC_Purple);
return 0;
}
以上。
|