windows和Linux都可用的一个类...用来设置颜色,没有太复杂。简单够用吧。
#ifdef _WIN32#includeclass 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----4940:黑41:深红42:绿43:黄色44:蓝色45:紫色46:深绿47:白色字颜色:30-----------3930:黑31:红32:绿33:黄34:蓝色35:紫色36:深绿37:白色