VC控制台程序中字体颜色和背景设置

在VC中开发程序,有时候为了使开发的控制台程序更加漂亮,我们需要设置显示字体的颜色和窗口背景颜色,针对本人实际开发经历,写一篇文章保留参考。



<span style="font-size:14px;">1.改变整个控制台的颜色
用 system("color 0A");
其中color后面的0是背景色代号,A是前景色代号。各颜色代码如下:
0=黑色
1=蓝色
2=绿色
3=湖蓝色
4=红色
5=紫色
6=黄色
7=白色
8=灰色
9=淡蓝色
A=淡绿色
B=淡浅绿色
C=淡红色
D=淡紫色
E=淡黄色
F=亮白色
2.改变下一个输出或者输入字体和背景的颜色
采用SetConsoleTextAttribute函数,如
White on Black:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
Red on Black:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_RED);
Green on Black:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_GREEN);
Yellow on Black:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_RED | FOREGROUND_GREEN);
Blue on Black:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_BLUE);
Magenta on Black:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_RED | FOREGROUND_BLUE);
Cyan on Black:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
            FOREGROUND_GREEN | FOREGROUND_BLUE);
Black on Gray:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
            BACKGROUND_INTENSITY);
Black on White:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
            FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
Red on White:
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
            FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
            FOREGROUND_RED);
等等。
需要引入头文件:
windows.h
函数原型:
BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes);
wAttributes 的取值含义对应如下:
Attribute                          Meaning
FOREGROUND_BLUE            Text color contains blue.
FOREGROUND_GREEN           Text color contains green.
FOREGROUND_RED             Text color contains red.
FOREGROUND_INTENSITY       Text color is intensified.
BACKGROUND_BLUE            Background color contains blue.
BACKGROUND_GREEN           Background color contains green.
BACKGROUND_RED             Background color contains red.
BACKGROUND_INTENSITY       Background color is intensified.
COMMON_LVB_LEADING_BYTE    Leading byte.
COMMON_LVB_TRAILING_BYTE   Trailing byte.
COMMON_LVB_GRID_HORIZONTAL Top horizontal.
COMMON_LVB_GRID_LVERTICAL  Left vertical.
COMMON_LVB_GRID_RVERTICAL  Right vertical.
COMMON_LVB_REVERSE_VIDEO   Reverse foreground and background attributes.
COMMON_LVB_UNDERSCORE      Underscore.


#include <stdio.h>
#include <windows.h>

int main(void)
{
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED | FOREGROUND_GREEN);
   printf("TestA");
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
   printf("TestB");
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
   printf("TestC\n");

   getchar();
   return 0;
}
</span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值