Print Coloured Text to Console in C++

前端 未结 4 1445
眼角桃花
眼角桃花 2020-12-31 21:38

I would like to write a Console class that can output coloured text to the console.

So I can do something like (basically a wrapper for printf):

Cons         


        
相关标签:
4条回答
  • 2020-12-31 22:12
    #include <windows.h>
    #include <iostream.h>
    using namespace std;
    
    int main()
    {
        HANDLE hOut;
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_RED);
        cout << "Red     " << flush;
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_RED |
                                BACKGROUND_INTENSITY);
        cout << "Red     " << endl;
    
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_GREEN);
        cout << "Green   " << flush;
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_GREEN |
                                BACKGROUND_INTENSITY);
        cout << "Green   " << endl;
    
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_BLUE);
        cout << "Blue    " << flush;
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_BLUE |
                                BACKGROUND_INTENSITY);
        cout << "Blue    " << endl;
    
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_RED | 
                                BACKGROUND_GREEN);
        cout << "Yellow  " << flush;
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_RED | 
                                BACKGROUND_GREEN |
                                BACKGROUND_INTENSITY);
        cout << "Yellow  " << endl;
    
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_GREEN | 
                                BACKGROUND_BLUE);
        cout << "Cyan    " << flush;
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_GREEN | 
                                BACKGROUND_BLUE |
                                BACKGROUND_INTENSITY);
        cout << "Cyan    " << endl;
    
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_BLUE | 
                                BACKGROUND_RED);
        cout << "Magenta " << flush;
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_BLUE | 
                                BACKGROUND_RED |
                                BACKGROUND_INTENSITY);
        cout << "Magenta " << endl;
    
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_RED | 
                                BACKGROUND_GREEN | 
                                BACKGROUND_BLUE);
        cout << "White   " << flush;
        SetConsoleTextAttribute(hOut,
                                BACKGROUND_RED | 
                                BACKGROUND_GREEN | 
                                BACKGROUND_BLUE |
                                BACKGROUND_INTENSITY);
        cout << "White   " << endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-31 22:12

    use these functions

    enum c_color{BLACK=30,RED=31,GREEN=32,YELLOW=33,BLUE=34,MAGENTA=35,CYAN=36,WHITE=37};
    enum c_decoration{NORMAL=0,BOLD=1,FAINT=2,ITALIC=3,UNDERLINE=4,RIVERCED=26,FRAMED=51};
    void pr(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
      cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m";
    }
    
    void prl(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
       cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m"<<endl;
    }
    
    0 讨论(0)
  • 2020-12-31 22:15

    Check out this guide. I would make a custom manipulator so I could do something like:

    std::cout << "standard text" << setcolour(red) << "red text" << std::endl;
    

    Here's a small guide on how to implement your own manipulator.

    A quick code example:

    #include <iostream>
    #include <windows.h>
    #include <iomanip>
    
    using namespace std;
    
    enum colour { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
    
    struct setcolour
    {
       colour _c;
       HANDLE _console_handle;
    
    
           setcolour(colour c, HANDLE console_handle)
               : _c(c), _console_handle(0)
           { 
               _console_handle = console_handle;
           }
    };
    
    // We could use a template here, making it more generic. Wide streams won't
    // work with this version.
    basic_ostream<char> &operator<<(basic_ostream<char> &s, const setcolour &ref)
    {
        SetConsoleTextAttribute(ref._console_handle, ref._c);
        return s;
    }
    
    int main(int argc, char *argv[])
    {
        HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);
        cout << "standard text" << setcolour(RED, chandle) << " red text" << endl;
    
        cin.get();
    }
    
    0 讨论(0)
  • 2020-12-31 22:24

    I did a search for "c++ console write colored text" and came up with this page at about 4 or 5. As the site has a copy & paste section I thought I'd post it here (another question on link rot also prompted this):

    #include <stdlib.h>
    #include <windows.h>
    #include <iostream>
    
    using namespace std;
    
    enum Color { DBLUE=1,GREEN,GREY,DRED,DPURP,BROWN,LGREY,DGREY,BLUE,LIMEG,TEAL,
        RED,PURPLE,YELLOW,WHITE,B_B };
    /* These are the first 16 colors anyways. You test the other hundreds yourself.
       After 15 they are all combos of different color text/backgrounds. */
    
    bool quit;
    
    void col(unsigned short color)
    {
        HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hcon,color);
    }
    
    istream &operator>> ( istream &in, Color &c )
    {
        int tint;
        cin >> tint;
        if (tint==-1) quit=true;
        c=(Color)tint;
    }
    
    int main()
    {
        do {
            col(7); // Defaults color for each round.
            cout << "Enter a color code, or -1 to quit... ";
            Color y;
            cin >> y; // Notice that >> is defined above for Color types.
            col(y); // Sets output color to y.
            if (!quit) cout << "Color: " << (int)y << endl;
        } while (!quit);
        return 0;
    }
    

    For C# there's this page

    0 讨论(0)
提交回复
热议问题