zoukankan      html  css  js  c++  java
  • 利用SetConsoleTextAttribute函数设置控制台颜色

    控制台单调的颜色只有黑白两种,看起来多少难免有些单调。在没学图形化编程之前,每天看着控制台程序难免会枯燥无味。
    不过,利用SetConsoleTextAttribute函数可以设置控制台的前景色和背景色。

    闲言少叙,书归正传。

    ①效果图:(颜色可以混合,遵循配色原理)

    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
        HANDLE hOut;
    
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED | 
                                FOREGROUND_GREEN);
        cout << "This text is yellow." << endl;
    
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_GREEN | 
                                FOREGROUND_BLUE);
        cout << "This text is cyan." << endl;
    
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_BLUE | 
                                FOREGROUND_RED);
        cout << "This text is magenta." << endl;
    
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED | 
                                FOREGROUND_GREEN | 
                                FOREGROUND_BLUE);
        cout << "This text is white." << endl;
    
    
        return 0;
    }
    ②效果图:(明暗颜色)

    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    int main()
    {
        HANDLE hOut;
    
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED);
        cout << "Red     " << flush;
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED |
                                FOREGROUND_INTENSITY);
        cout << "Red" << endl;
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_GREEN);
        cout << "Green   " << flush;
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_GREEN |
                                FOREGROUND_INTENSITY);
        cout << "Green" << endl;
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_BLUE);
        cout << "Blue    " << flush;
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_BLUE |
                                FOREGROUND_INTENSITY);
        cout << "Blue" << endl;
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED | 
                                FOREGROUND_GREEN);
        cout << "Yellow  " << flush;
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED | 
                                FOREGROUND_GREEN |
                                FOREGROUND_INTENSITY);
        cout << "Yellow" << endl;
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_GREEN | 
                                FOREGROUND_BLUE);
        cout << "Cyan    " << flush;
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_GREEN | 
                                FOREGROUND_BLUE |
                                FOREGROUND_INTENSITY);
        cout << "Cyan" << endl;
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_BLUE | 
                                FOREGROUND_RED);
        cout << "Magenta " << flush;
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_BLUE | 
                                FOREGROUND_RED |
                                FOREGROUND_INTENSITY);
        cout << "Magenta" << endl;
    
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED | 
                                FOREGROUND_GREEN | 
                                FOREGROUND_BLUE);
        cout << "White   " << flush;
        SetConsoleTextAttribute(hOut,
                                FOREGROUND_RED | 
                                FOREGROUND_GREEN | 
                                FOREGROUND_BLUE |
                                FOREGROUND_INTENSITY);
        cout << "White" << endl;
    
        return 0;
    }
    ③背景颜色:

    #include <windows.h>
    #include <iostream>
    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;
    }



  • 相关阅读:
    layui flow loading占位图实现方法
    layui弹出层layer的area过大被遮挡
    layui 复选框checkbox 实现全选全选
    axios 设置headers token
    elementUI vue this.$confirm 和el-dialog 弹出框 移动
    vue + axios + formdata 上传文件带参数的爬坑之路
    Java四舍五入时保留指定小数位数
    List containsKey 和Map contains 判断集合中是否包含某个值
    BigDecimal 基本使用 比较大小和加减乘除
    springMVC返回json数据乱码问
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835114.html
Copyright © 2011-2022 走看看