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

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

    闲言少叙,书归正传。

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

    [cpp]  view plain copy
    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. }  



    ②效果图:(明暗颜色)

    [cpp]  view plain copy
    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. }  

    ③背景颜色:

    [cpp]  view plain copy
    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. }  

    以上的介绍大致上能满足需要了。如果需要更多的颜色使用方法。请参考
    http://charon.bdeb.qc.ca/docs/cpp/www.adrianxw.dk/SoftwareSite/Consoles/Consoles4.html

    后续会更新自定义颜色的原理和方法。。。

    http://blog.csdn.net/odaynot/article/details/7722240

  • 相关阅读:
    如何使用命令行备份SAP HANA数据库
    Rootkit介绍
    web渗透测试基本步骤
    IIS服务器的安全保护措施
    渗透测试的流程
    Kali Linux之常见后门工具介绍
    中间人攻击(MITM)之数据截获原理
    口令破解工具
    常见渗透测试工具集成系统简介
    Kali Linux之使用SET快捷生成钓鱼网站方法
  • 原文地址:https://www.cnblogs.com/byfei/p/14104368.html
Copyright © 2011-2022 走看看