欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使c++控制台的文字变得多彩

程序员文章站 2022-05-29 23:11:53
...

使c++控制台的文字变得多彩

通常我们使用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;
}

使c++控制台的文字变得多彩
代码下载,其实也没什么代码,就是上面那个consolecolor.hpp文件,拷贝一下,然后include就可以直接用了。