利用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;  
}  

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏码云1024

c++ 平衡二叉树的实现

1284
来自专栏转载gongluck的CSDN博客

cocos2dx 消灭星星

#include "GamePopStar.h" #include "StarSprite.h" USING_NS_CC; Scene* GamePopSt...

3879
来自专栏coding...

iOS开发实战-基于SpriteKit的FlappyBird小游戏写在前面SpriteKit简介效果分析代码关于游戏上架TipsDemo地址还没结束

蛋疼广电粽菊要求国内游戏必须备案... 我们只是想上个小游戏而已~难道还要再等个大半个月去备案么? Apple也妥协了 在备注那里要求中国区上架游戏必须填写...

1352
来自专栏技术小黑屋

Check if a Thread Is Main Thread in Android

How to check a certain thread is the main one or not in Android? You may say it ...

1071
来自专栏c#开发者

简体-繁体内码转换API

 public class ANSIConversionAPI     {         const int SIMPLIFIED_CHINESE =...

4005
来自专栏智能计算时代

3 Lessons IBM's Watson Can Teach Us About Our Brains' Biases

COGNITIVE COMPUTING IS TRANSFORMING THE WAY WE WORK. IT ALSO OFFERS A WINDOW TO ...

2767
来自专栏数据库新发现

在RAC环境中如何管理日志(redolog file)组

http://www.eygle.com/faq/How.To.Mangement.redologfile.in.Rac.Raw.htm

972
来自专栏开心的学习之路

03 Jme3和Nifty1.4.2中文显示

用JMonkey最大的问题就是中文问题,IDE不是中文没关系,反正可以迁移到Idea里,但是打包发布的项目以及Nifty做的GUI里没有中文就心塞塞了。好在找到...

37410
来自专栏一个会写诗的程序员的博客

org.hamcrest.Matchers 类的方法签名

662
来自专栏pydata

python asynchrous network

641

扫码关注云+社区