控制台界面控制(五):文字颜色

本文为转载文档,原文档链接:http://blog.csdn.net/bnb45/article/details/7994025

 

 

设置字符属性

VC控制台提供了一个设置文本属性的函数,它的作用包括设置文本颜色,设置文本背景颜色,以及下划线等。

 

[cpp] view plaincopy

  1. BOOL SetConsoleTextAttribute(  
  2.     HANDLE  hConsoleOutput,     // 句柄  
  3.     WORD    wAttributes         // 文本属性  
  4. );  

文本属性包含以下内容:

 

[cpp] view plaincopy

  1. FOREGROUND_BLUE         // 文本颜色包含蓝色  
  2. FOREGROUND_GREEN        // 文本颜色包含绿色  
  3. FOREGROUND_RED          // 文本颜色包含红色  
  4.   
  5. FOREGROUND_INTENSITY    // 文本颜色加亮  
  6.   
  7. BACKGROUND_BLUE         // 背景含蓝色  
  8. BACKGROUND_GREEN        // 背景含绿色  
  9. BACKGROUND_RED          // 背景含红色  
  10.   
  11. BACKGROUND_INTENSITY    // 背景加亮  
  12.   
  13. COMMON_LVB_LEADING_BYTE     // 首字节  
  14. COMMON_LVB_TRAILING_BYTE    // 末字节  
  15. COMMON_LVB_GRID_HORIZONTAL  // 首行  
  16. COMMON_LVB_GRID_LVERTICAL   // 左列  
  17. COMMON_LVB_GRID_RVERTICAL   // 右列  
  18. COMMON_LVB_REVERSE_VIDEO    // 翻转前景及背景属性  
  19. COMMON_LVB_UNDERSCORE       // 下划线  

 

设置文本颜色及设置文本背景颜色

就目前看来,我只需要两个功能就可以了。一个是对输出文本的颜色控制,另一个是文本的背景颜色。通过一些方法把它们从原先的函数中分离开来。

 

[cpp] view plaincopy

  1. // ---- 在头文件中枚举基本颜色常数,使用它们来设置颜色  
  2. enum TEXT_COLOR{  
  3.     BLACK   = 0,  
  4.     RED     = 1,  
  5.     BLUE    = 2,  
  6.     GREEN   = 4,  
  7. };  

[cpp] view plaincopy

  1. public:  
  2. // ---- 设置文本(背景)颜色  
  3. // @param   int             [in] 颜色属性,TEXT_COLOR常量组合使用时会转化为int  
  4. // @param   bool = flase    [in] 颜色是否加亮  
  5. void SetTextColor(int color, bool bIntensity=false);  
  6. void SetTextBackgroundColor(int color, bool bIntencity=false);  
  7.   
  8. private:  
  9. // -- 头文件中声明两个成员变量,用来存放当前文本的颜色信息  
  10. // -- 避免设置文本颜色的时候无法设置背景颜色的情况  
  11. WORD wTextColor;  
  12. WORD wTextBackgroundColor;  

代码实现

[cpp] view plaincopy

  1. // ---- 设置输出的文本颜色  
  2. void CMyConsole::SetTextColor(int color, bool bIntensity)  
  3. {  
  4.     wTextColor = 0;  
  5.   
  6.     if (color & RED)  
  7.     {  
  8.         wTextColor |= FOREGROUND_RED;  
  9.     }  
  10.     if (color & BLUE)  
  11.     {  
  12.         wTextColor |= FOREGROUND_BLUE;  
  13.     }  
  14.     if (color & GREEN)  
  15.     {  
  16.         wTextColor |= FOREGROUND_GREEN;  
  17.     }  
  18.     if (bIntensity)  
  19.     {  
  20.         wTextColor |= FOREGROUND_INTENSITY;  
  21.     }  
  22.   
  23.     // -- 设置 WriteConsole 等函数的字符属性  
  24.     // @param   HANDLE      [in] 控制台句柄  
  25.     // @param   WORD        [in] 文本属性  
  26.     // #return  bool               
  27.     SetConsoleTextAttribute(hOut, wTextColor | wTextBackgroundColor);  
  28. };  
  29.   
  30. // ---- 设置输出文本的背景颜色  
  31. void CMyConsole::SetTextBackgroundColor(int color, bool bIntensity)  
  32. {  
  33.     wTextBackgroundColor = 0;  
  34.   
  35.     if (color & RED)  
  36.     {  
  37.         wTextBackgroundColor |= BACKGROUND_RED;  
  38.     }  
  39.     if (color & BLUE)  
  40.     {  
  41.         wTextBackgroundColor |= BACKGROUND_BLUE;  
  42.     }  
  43.     if (color & GREEN)  
  44.     {  
  45.         wTextBackgroundColor |= BACKGROUND_GREEN;  
  46.     }  
  47.     if (bIntensity)  
  48.     {  
  49.         wTextBackgroundColor |= BACKGROUND_INTENSITY;  
  50.     }  
  51.   
  52.     // -- 设置 WriteConsole 等函数的字符属性  
  53.     SetConsoleTextAttribute(hOut, wTextColor | wTextBackgroundColor);  
  54. };  

实例测试

[cpp] view plaincopy

  1. int main()  
  2. {  
  3.     CMyConsole myConsole;  
  4.   
  5.     myConsole.SetTextColor(RED);  
  6.     printf("这是红字\n");  
  7.     myConsole.SetTextColor(RED, true);  
  8.     printf("这是加亮红字\n");  
  9.     myConsole.SetTextColor(RED|BLUE, true);  
  10.     printf("这是加亮紫字\n");  
  11.     myConsole.SetTextColor(BLACK);  
  12.     myConsole.SetTextBackgroundColor(RED|BLUE|GREEN);  
  13.     printf("这是白底黑字\n");  
  14.   
  15.     myConsole.SetTextColor(GREEN);  
  16.     myConsole.SetTextBackgroundColor(RED|BLUE|GREEN, true);  
  17.     printf("加亮白底绿字 ");  
  18.     system("pause");  
  19.     return 0;  
  20. }  

0

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值