使c++控制台的文字变得多彩
2017-12-06 09:43
183 查看
使c++控制台的文字变得多彩
通常我们使用c++控制台编程面对的黑框和白色单调的文字,如下:如何让输出的信息有颜色呢?比如cerr输出的就是红色,比如cout输出的就是绿色。
接下来,我们实现着色功能。下面借鉴了C++的操纵器策略。
//consolecolor.hpp这是着色的实现头文件 #pragma once #include<Windows.h>//调用win32API函数 #include<iostream>//调用flush成员函数,首先刷新缓冲区 namespace color//命名空间为color,防止名字冲突 { //颜色常量定义,分为掩码,前景色,背景色 static const WORD bgMask(BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY); static const WORD fgMask(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY); static const WORD fgBlack(0); static const WORD fgLoRed(FOREGROUND_RED); static const WORD fgLoGreen(FOREGROUND_GREEN); static const WORD fgLoBlue(FOREGROUND_BLUE); static const WORD fgLoCyan(fgLoGreen | fgLoBlue); static const WORD fgLoMagenta(fgLoRed | fgLoBlue); static const WORD fgLoYellow(fgLoRed | fgLoGreen); static const WORD fgLoWhite(fgLoRed | fgLoGreen | fgLoBlue); static const WORD fgGray(fgBlack | FOREGROUND_INTENSITY); static const WORD fgHiWhite(fgLoWhite | FOREGROUND_INTENSITY); static const WORD fgHiBlue(fgLoBlue | FOREGROUND_INTENSITY); static const WORD fgHiGreen(fgLoGreen | FOREGROUND_INTENSITY); static const WORD fgHiRed(fgLoRed | FOREGROUND_INTENSITY); static const WORD fgHiCyan(fgLoCyan | FOREGROUND_INTENSITY); static const WORD fgHiMagenta(fgLoMagenta | FOREGROUND_INTENSITY); static const WORD fgHiYellow(fgLoYellow | FOREGROUND_INTENSITY); static const WORD bgBlack(0); static const WORD bgLoRed(BACKGROUND_RED); static const WORD bgLoGreen(BACKGROUND_GREEN); static const WORD bgLoBlue(BACKGROUND_BLUE); static const WORD bgLoCyan(bgLoGreen | bgLoBlue); static const WORD bgLoMagenta(bgLoRed | bgLoBlue); static const WORD bgLoYellow(bgLoRed | bgLoGreen); static const WORD bgLoWhite(bgLoRed | bgLoGreen | bgLoBlue); static const WORD bgGray(bgBlack | BACKGROUND_INTENSITY); static const WORD bgHiWhite(bgLoWhite | BACKGROUND_INTENSITY); static const WORD bgHiBlue(bgLoBlue | BACKGROUND_INTENSITY); static const WORD bgHiGreen(bgLoGreen | BACKGROUND_INTENSITY); static const WORD bgHiRed(bgLoRed | BACKGROUND_INTENSITY); static const WORD bgHiCyan(bgLoCyan | BACKGROUND_INTENSITY); static const WORD bgHiMagenta(bgLoMagenta | BACKGROUND_INTENSITY); static const WORD bgHiYellow(bgLoYellow | BACKGROUND_INTENSITY); //设置颜色的实现函数,主要是调用win32的API和一些位运算。 void setConsoleColor(WORD mask, WORD c) { static HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); if(hStdout==NULL)return; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(hStdout, &csbi); csbi.wAttributes &= ~mask; csbi.wAttributes |= c; SetConsoleTextAttribute(hStdout, csbi.wAttributes); } //用到模板是为了通用,不仅cout,cerr可以染色,wcout,wcerr等也可以着色,自己定义的流只要满足模板都可以。 template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_white(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgLoWhite); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_blue(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgHiBlue); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_green(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgHiGreen); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_red(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgHiRed); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_cyan(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgHiCyan); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_magenta(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgHiMagenta); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_yellow(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgHiYellow); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& fg_black(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(fgMask, fgBlack); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_white(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgLoWhite); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_blue(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgHiBlue); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_green(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgHiGreen); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_red(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgHiRed); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_cyan(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgHiCyan); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_magenta(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgHiMagenta); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_yellow(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgHiYellow); return os; } template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& bg_black(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgBlack); return os; } //清空屏幕,还原本色 template<typename charT, typename traits> inline std::basic_ostream<charT, traits>& clr(std::basic_ostream<charT, traits>& os) { os.flush(); setConsoleColor(bgMask, bgBlack); setConsoleColor(fgMask, fgLoWhite); system("cls"); return os; } }
实现文件就是上面的consolecolor.hpp,因为主要是模板函数所以函数定义都放在h文件里面。下面我们来看看怎么用。
#include<iostream> #include"consolecolor.h" using namespace std; using namespace color; int main() { cout << 1234 << endl; cout<<fg_green<<123456<<endl; cerr<<1233<<fg_red<<33445<<endl; cout<<bg_cyan<<3333<<endl; wcout<<12334<<endl; wcout<<fg_yellow<<123455<<endl; wcout<<bg_white<<fg_green<<L"谢谢阅读"<<endl; system("pause"); return 0; }
代码下载,其实也没什么代码,就是上面那个consolecolor.hpp文件,拷贝一下,然后include就可以直接用了。
相关文章推荐
- C++控制台输出彩色文字
- C/C++ 修改控制台程序文字颜色
- C/C++ 修改控制台程序文字颜色
- C++运行结果中让控制台停住的办法
- C++ GDI+ DrawString 保存字符串文字为透明图片
- C++ 语言中的文字常量区
- 用CSS打造多彩文字链接
- c++控制台应用程序
- OpenCV 2.4+ C++ SVM文字识别
- 让C++控制台程序停下来,实现暂停功能
- [C/C++]宽字符与控制台程序
- 转:c/c++里的 堆区 栈区 静态区 文字常量区 程序代码区
- C/C++ 获得控制台输入的3种方法
- 图书管理程序-完美控制台c++(cpp)
- c++ 控制台流和字符串流
- 控制台下实现简单的文本菜单及一般题目的主框架(可用于C/C++、特别是数据结构实验)
- 在vs2008 C++下调试控制台程序的心得及技巧
- C语言控制台文字颜色
- C++控制台操作(基本操作的代码)
- 控制台实现闪烁带颜色文字