引用自CSDN 学习记录之用 侵删
引入如下代码使程序运行结果一目了然
#include <Windows.h>
#include <iostream>
static void SetConsoleColor(WORD wAttribute)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, wAttribute);
}
inline std::ostream& defcolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
return ostr;
}
inline std::ostream& greencolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
return ostr;
}
inline std::ostream& bluecolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
return ostr;
}
inline std::ostream& redcolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
return ostr;
}
inline std::ostream& lredcolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_RED);
return ostr;
}
inline std::ostream& yellowcolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
return ostr;
}
inline std::ostream& magenta(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
return ostr;
}
inline std::ostream& cyancolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
return ostr;
}
inline std::ostream& graycolor(std::ostream& ostr)
{
SetConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);
return ostr;
}
输出格式如下
std::cout << redcolor << "I am RED" << defcolor << std::endl;
std::cout << bluecolor << "I am BLUE" << defcolor << std::endl;
网友评论