利用SetConsoleTextAttribute函数设置控制台颜色 作者:odaynot

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


控制台单调的颜色只有黑白两种,看起来多少难免有些单调。在没学图形化编程之前,每天看着控制台程序难免会枯燥无味。
不过,利用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;  //cout<<flush表示将缓冲区的内容马上送进cout,把输出缓冲区刷新。
  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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值