美文网首页
命令行SetConsoleColor

命令行SetConsoleColor

作者: 王晓宇_xiaoyuwang | 来源:发表于2016-10-23 15:41 被阅读0次

    引用自CSDN 学习记录之用 侵删

    引入如下代码使程序运行结果一目了然

    #include <Windows.h>
    #include <iostream>
    
    static void  SetConsoleColor(WORD wAttribute)
    {
        HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(handle, wAttribute);
    }
    
    inline std::ostream&  defcolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
        return ostr;
    }
    
    inline std::ostream&  greencolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        return ostr;
    }
    
    inline std::ostream&  bluecolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        return ostr;
    }
    
    inline std::ostream&  redcolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
        return ostr;
    }
    
    inline std::ostream&  lredcolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_RED);
        return ostr;
    }
    
    inline std::ostream& yellowcolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        return ostr;
    }
    
    inline std::ostream& magenta(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
        return ostr;
    }
    
    inline std::ostream& cyancolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
        return ostr;
    }
    
    inline std::ostream& graycolor(std::ostream& ostr)
    {
        SetConsoleColor(FOREGROUND_INTENSITY | FOREGROUND_INTENSITY);
        return ostr;
    }
    

    输出格式如下

    std::cout << redcolor << "I am RED" << defcolor << std::endl;
    std::cout << bluecolor << "I am BLUE" << defcolor << std::endl;
    

    相关文章

      网友评论

          本文标题:命令行SetConsoleColor

          本文链接:https://www.haomeiwen.com/subject/ielkuttx.html