我是靠谱客的博主 干净白羊,最近开发中收集的这篇文章主要介绍C/C++控制台输出时设置字体及背景颜色,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

C/C++控制台输出时设置字体及背景颜色

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 的取值含义对应如下:

AttributeMeaning
FOREGROUND_BLUEText color contains blue.
FOREGROUND_GREENText color contains green.
FOREGROUND_REDText color contains red.
FOREGROUND_INTENSITYText color is intensified.
BACKGROUND_BLUEBackground color contains blue.
BACKGROUND_GREENBackground color contains green.
BACKGROUND_REDBackground color contains red.
BACKGROUND_INTENSITYBackground color is intensified.
COMMON_LVB_LEADING_BYTELeading byte.
COMMON_LVB_TRAILING_BYTETrailing byte.
COMMON_LVB_GRID_HORIZONTALTop horizontal.
COMMON_LVB_GRID_LVERTICALLeft vertical.
COMMON_LVB_GRID_RVERTICALRight vertical.
COMMON_LVB_REVERSE_VIDEOReverse foreground and background attributes.
COMMON_LVB_UNDERSCOREUnderscore.


实例:


#include <stdio.h>
#include <windows.h>
#include <iostream>
using namespace std;
int main(void)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED | FOREGROUND_GREEN);
cout<<"zhangzhe"<<endl;
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");
getchar();
return 0;
}


最后

以上就是干净白羊为你收集整理的C/C++控制台输出时设置字体及背景颜色的全部内容,希望文章能够帮你解决C/C++控制台输出时设置字体及背景颜色所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部