windows和Linux都可用的一个类...用来设置颜色,没有太复杂。简单够用吧。
#ifdef _WIN32
#include <Windows.h>
class FontColor
{
public:
FontColor()
{
m_hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
}
void SetColor(int nColor)
{
SetConsoleTextAttribute(m_hStdOut, nColor);
}
void UnSetColor()
{
SetConsoleTextAttribute(m_hStdOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
~FontColor()
{
}
private:
HANDLE m_hStdOut;
};
#else
class FontColor
{
public:
FontColor()
{
m_bUnset = true;
}
void SetColor(int nColor)
{
printf("\033[%dm", nColor%10 + 30);
m_bUnset = false;
}
void UnSetColor()
{
if(!m_bUnset)
{
printf("\033[0m");
m_bUnset = true;
}
}
~FontColor()
{
UnSetColor();
}
private:
bool m_bUnset;
};
#endif
输出效果:
Linux颜色列表表示(本类做了点特殊处理):
字背景颜色范围:40----49
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色
字颜色:30-----------39
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色