• 控制台颜色


    控制台单调的颜色只有黑白两种,看起来多少有点单调。其实,可以利用SetConsoleTextAttribute函数设置控制台的前景色和背景色。

    该函数需要头文件 #include <windows.h>

    一、颜色可以混合,遵循配色原理

    1.    #include <windows.h>  
    2.    #include <iostream>  
    3.    using namespace std;  
    4.      
    5.      
    6.    int main()  
    7.    {  
    8.        HANDLE hOut;  
    9.      
    10.      
    11.        hOut = GetStdHandle(STD_OUTPUT_HANDLE);  
    12.      
    13.      
    14.        SetConsoleTextAttribute(hOut,  
    15.                                FOREGROUND_RED |   
    16.                                FOREGROUND_GREEN);  
    17.        cout << "This text is yellow." << endl;  
    18.      
    19.      
    20.        SetConsoleTextAttribute(hOut,  
    21.                                FOREGROUND_GREEN |   
    22.                                FOREGROUND_BLUE);  
    23.        cout << "This text is cyan." << endl;  
    24.      
    25.      
    26.        SetConsoleTextAttribute(hOut,  
    27.                                FOREGROUND_BLUE |   
    28.                                FOREGROUND_RED);  
    29.        cout << "This text is magenta." << endl;  
    30.      
    31.      
    32.        SetConsoleTextAttribute(hOut,  
    33.                                FOREGROUND_RED |   
    34.                                FOREGROUND_GREEN |   
    35.                                FOREGROUND_BLUE);  
    36.        cout << "This text is white." << endl;  
    37.      
    38.      
    39.        return 0;  
    40.    }  

    二、明暗颜色

    1.    #include <windows.h>  
    2.    #include <iostream>  
    3.    using namespace std;  
    4.      
    5.    int main()  
    6.    {  
    7.        HANDLE hOut;  
    8.      
    9.        hOut = GetStdHandle(STD_OUTPUT_HANDLE);  
    10.      
    11.        SetConsoleTextAttribute(hOut,  
    12.                                FOREGROUND_RED);  
    13.        cout << "Red     " << flush;  
    14.        SetConsoleTextAttribute(hOut,  
    15.                                FOREGROUND_RED |  
    16.                                FOREGROUND_INTENSITY);  
    17.        cout << "Red" << endl;  
    18.      
    19.        SetConsoleTextAttribute(hOut,  
    20.                                FOREGROUND_GREEN);  
    21.        cout << "Green   " << flush;  
    22.        SetConsoleTextAttribute(hOut,  
    23.                                FOREGROUND_GREEN |  
    24.                                FOREGROUND_INTENSITY);  
    25.        cout << "Green" << endl;  
    26.      
    27.        SetConsoleTextAttribute(hOut,  
    28.                                FOREGROUND_BLUE);  
    29.        cout << "Blue    " << flush;  
    30.        SetConsoleTextAttribute(hOut,  
    31.                                FOREGROUND_BLUE |  
    32.                                FOREGROUND_INTENSITY);  
    33.        cout << "Blue" << endl;  
    34.      
    35.        SetConsoleTextAttribute(hOut,  
    36.                                FOREGROUND_RED |   
    37.                                FOREGROUND_GREEN);  
    38.        cout << "Yellow  " << flush;  
    39.        SetConsoleTextAttribute(hOut,  
    40.                                FOREGROUND_RED |   
    41.                                FOREGROUND_GREEN |  
    42.                                FOREGROUND_INTENSITY);  
    43.        cout << "Yellow" << endl;  
    44.      
    45.        SetConsoleTextAttribute(hOut,  
    46.                                FOREGROUND_GREEN |   
    47.                                FOREGROUND_BLUE);  
    48.        cout << "Cyan    " << flush;  
    49.        SetConsoleTextAttribute(hOut,  
    50.                                FOREGROUND_GREEN |   
    51.                                FOREGROUND_BLUE |  
    52.                                FOREGROUND_INTENSITY);  
    53.        cout << "Cyan" << endl;  
    54.      
    55.        SetConsoleTextAttribute(hOut,  
    56.                                FOREGROUND_BLUE |   
    57.                                FOREGROUND_RED);  
    58.        cout << "Magenta " << flush;  
    59.        SetConsoleTextAttribute(hOut,  
    60.                                FOREGROUND_BLUE |   
    61.                                FOREGROUND_RED |  
    62.                                FOREGROUND_INTENSITY);  
    63.        cout << "Magenta" << endl;  
    64.      
    65.        SetConsoleTextAttribute(hOut,  
    66.                                FOREGROUND_RED |   
    67.                                FOREGROUND_GREEN |   
    68.                                FOREGROUND_BLUE);  
    69.        cout << "White   " << flush;  
    70.        SetConsoleTextAttribute(hOut,  
    71.                                FOREGROUND_RED |   
    72.                                FOREGROUND_GREEN |   
    73.                                FOREGROUND_BLUE |  
    74.                                FOREGROUND_INTENSITY);  
    75.        cout << "White" << endl;  
    76.      
    77.        return 0;  
    78.    }

    三、背景颜色

    1.    #include <windows.h>  
    2.    #include <iostream>  
    3.    using namespace std;  
    4.      
    5.    int main()  
    6.    {  
    7.        HANDLE hOut;  
    8.      
    9.        hOut = GetStdHandle(STD_OUTPUT_HANDLE);  
    10.      
    11.        SetConsoleTextAttribute(hOut,  
    12.                                BACKGROUND_RED);  
    13.        cout << "Red     " << flush;  
    14.        SetConsoleTextAttribute(hOut,  
    15.                                BACKGROUND_RED |  
    16.                                BACKGROUND_INTENSITY);  
    17.        cout << "Red     " << endl;  
    18.      
    19.        SetConsoleTextAttribute(hOut,  
    20.                                BACKGROUND_GREEN);  
    21.        cout << "Green   " << flush;  
    22.        SetConsoleTextAttribute(hOut,  
    23.                                BACKGROUND_GREEN |  
    24.                                BACKGROUND_INTENSITY);  
    25.        cout << "Green   " << endl;  
    26.      
    27.        SetConsoleTextAttribute(hOut,  
    28.                                BACKGROUND_BLUE);  
    29.        cout << "Blue    " << flush;  
    30.        SetConsoleTextAttribute(hOut,  
    31.                                BACKGROUND_BLUE |  
    32.                                BACKGROUND_INTENSITY);  
    33.        cout << "Blue    " << endl;  
    34.      
    35.        SetConsoleTextAttribute(hOut,  
    36.                                BACKGROUND_RED |  
    37.                                BACKGROUND_GREEN);  
    38.        cout << "Yellow  " << flush;  
    39.        SetConsoleTextAttribute(hOut,  
    40.                                BACKGROUND_RED |  
    41.                                BACKGROUND_GREEN |  
    42.                                BACKGROUND_INTENSITY);  
    43.        cout << "Yellow  " << endl;  
    44.      
    45.        SetConsoleTextAttribute(hOut,  
    46.                                BACKGROUND_GREEN |  
    47.                                BACKGROUND_BLUE);  
    48.        cout << "Cyan    " << flush;  
    49.        SetConsoleTextAttribute(hOut,  
    50.                                BACKGROUND_GREEN |  
    51.                                BACKGROUND_BLUE |  
    52.                                BACKGROUND_INTENSITY);  
    53.        cout << "Cyan    " << endl;  
    54.      
    55.        SetConsoleTextAttribute(hOut,  
    56.                                BACKGROUND_BLUE |  
    57.                                BACKGROUND_RED);  
    58.        cout << "Magenta " << flush;  
    59.        SetConsoleTextAttribute(hOut,  
    60.                                BACKGROUND_BLUE |  
    61.                                BACKGROUND_RED |  
    62.                                BACKGROUND_INTENSITY);  
    63.        cout << "Magenta " << endl;  
    64.      
    65.        SetConsoleTextAttribute(hOut,  
    66.                                BACKGROUND_RED |  
    67.                                BACKGROUND_GREEN |  
    68.                                BACKGROUND_BLUE);  
    69.        cout << "White   " << flush;  
    70.        SetConsoleTextAttribute(hOut,  
    71.                                BACKGROUND_RED |  
    72.                                BACKGROUND_GREEN |  
    73.                                BACKGROUND_BLUE |  
    74.                                BACKGROUND_INTENSITY);  
    75.        cout << "White   " << endl;  
    76.      
    77.        return 0;  
    78.    }  

    四、简化颜色函数

    #include <windows.h>
    
    void color(int b)
    {
        HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hConsole,b);
    }

    具体配色,参见下图:

    含义

    STD_INPUT_HANDLE

    标准输入的句柄

    STD_OUTPUT_HANDLE

    标准输出的句柄

    STD_ERROR_HANDLE

    标准错误的句柄

      

    wAttributes是用来设置颜色的参数:

    wAttributes

    颜色

    对应的值(b)

    FOREGROUND_BLUE

    字体颜色:蓝

    1

    FOREGROUND_GREEN

    字体颜色:绿

    2

    FOREGROUND_RED

    字体颜色:红

    4

    FOREGROUND_INTENSITY

    前景色高亮显示

    8

    BACKGROUND_BLUE

    背景颜色:蓝

    16

    BACKGROUND_GREEN

    背景颜色:绿

    32

    BACKGROUND_RED

    背景颜色:红

    64

    BACKGROUND_INTENSITY

    背景色高亮显示

    128

    也就是说SetConsoleTextAttribute函数是靠一个字节的低四来控制前景色,高四位来控制背景色。

     

    例如:

    color(4):字体为红;color(8+4):字体为亮红;

    color(6):字体为黄;color(8+6):字体为亮黄;

    背景色同理。

  • 相关阅读:
    css3 3d 转换
    css3 动画序列
    css3 动画
    2d 转换之缩放
    2d 转换中心点
    css3 书写 动画三角形
    2d 旋转
    2D转换
    伪元素 字体图标
    风陵01
  • 原文地址:https://www.cnblogs.com/moon-future/p/4911095.html
Copyright © 2020-2023  润新知