我是靠谱客的博主 老实白昼,最近开发中收集的这篇文章主要介绍C/C++改变控制台的字体颜色,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

当你接触编程的时候,是不是总是有这种感受:白字黑色的控制台总是很枯燥,正如你用大脑运行这段代码

#include <stdio.h>
#include <conio.h>
int main(int argc,char *argv[])
{
    printf("Hello wolrdn");
    getch();
    return 0;
}

渐渐的熟悉了电脑的基本的操作,学会了利用控制台的属性来修改程序的字体

可是什么时候才能写出来漂亮的界面:渐渐的学会了函数,终于知道了一个函数可以修改控制台的字体颜色

#include <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
    system("color FC");//产生亮白色
    printf("I am Bamboo!");
    system("color 70");//产生背景白色文字黑色
    return 0;
}

 color命令详细介绍

设置默认的控制台前景和背景颜色。

COLOR [attr]

  attr        指定控制台输出的颜色属性。

颜色属性由两个十六进制数字指定 -- 第一个
对应于背景,第二个对应于前景。每个数字
可以为以下任何值:

    0 = 黑色       8 = 灰色
    1 = 蓝色       9 = 淡蓝色
    2 = 绿色       A = 淡绿色
    3 = 浅绿色     B = 淡浅绿色
    4 = 红色       C = 淡红色
    5 = 紫色       D = 淡紫色
    6 = 黄色       E = 淡黄色
    7 = 白色       F = 亮白色

如果没有给定任何参数,此命令会将颜色还原到 CMD.EXE 启动时
的颜色。这个值来自当前控制台
窗口、/T 命令行开关或 DefaultColor 注册表
值。

如果尝试使用相同的
前景和背景颜色来执行
 COLOR 命令,COLOR 命令会将 ERRORLEVEL 设置为 1。

示例: "COLOR fc" 在亮白色上产生淡红色

这个时候你终于可以控制控制台的颜色了

可是有一个问题,控制台的颜色总是总是和语句运行之后的system('color 70')最后一个函数颜色修改的命令保持一致,似乎想把文字显示如csdn这样的高亮代码风格不太可能

?

现在为您送上一个函数

#include <stdio.h>
#include <stdlib.h> 
#include <windows.h>
int main()
{
	HANDLE handle;//创建句柄 
	handle=GetStdHandle(STD_OUTPUT_HANDLE);//取标准输入输出句柄 
	SetConsoleTextAttribute(handle,0xF);//字符与 color相同 
	printf("Hello worldn"); 
	SetConsoleTextAttribute(handle,0xFC);
	printf("Hello wolrdn");
	return 0;
}

setConsoleTextAttribute()函数的详细解释如下

BOOL SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes);

参数

consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);

GetStdHandle(nStdHandle)是返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄。

其参数nStdHandle的值为下面几种类型的一种:

含义
STD_INPUT_HANDLE标准输入的句柄
STD_OUTPUT_HANDLE标准输出的句柄
STD_ERROR_HANDLE标准错误的句柄

wAttributes是用来设置颜色的参数:

wAttributes颜色对应的值
FOREGROUND_BLUE字体颜色:蓝1
FOREGROUND_GREEN字体颜色:绿2
FOREGROUND_RED字体颜色:红4
FOREGROUND_INTENSITY前景色高亮显示8
BACKGROUND_BLUE背景颜色:蓝16
BACKGROUND_GREEN背景颜色:绿32
BACKGROUND_RED

背景颜色:红

64
BACKGROUND_INTENSITY背景色高亮显示128

也就是说SetConsoleTextAttribute函数是靠一个字节的低四来控制前景色,高四位来控制背景色。

其他写法

SetConsoleTextAttribute(handle,0x07);填十六进制数字,前面的数字代表背景色,后面的代表前景色

SetConsoleTextAttribute(handle,7);直接接十进制数字。

具体见右图。

应用举例

FOREGROUND_RED表示设置前景色为红色,即字体的颜色为红色;

FOREGROUND_INTENSITY 表示设置前景色为高亮显示,FOREGROUND_GREEN表示绿色,两个参数中间加"|"表示前景色为高亮绿色;

BACKGROUND_INTENSITY表示设置背景色为高亮显示,BACKGROUND_BLUE表示背景色为蓝色,BACKGROUND_INTENSITY |BACKGROUND_BLUE两个参数就使背景色为高亮蓝色。

要表示红色字体,白色背景可以使用SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |FOREGROUND_INTENSITY | FOREGROUND_RED|BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);

设置背景与字体都为高显BACKGROUND_INTENSITY |FOREGROUND_INTENSITY,设置字体颜色为FOREGROUND_RED,背景颜色为BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE。其实这里把这些数值加起来就是二进制的,11111100,即十六进制0xFC。所以设置白体红字字也可以写成:

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xFC)。

其他颜色可照此类推:

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 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);

注意:使用SetConsoleTextAttribute函数一定要加上windows.h。

最后送上一个大神写的改变控制台写的库

ColorfulConsole.h(实现全在该文件里面)

// ColorfulConsole.cpp : 瀹氫箟鎺у埗鍙板簲鐢ㄧ▼搴忕殑鍏ュ彛鐐广€?
//

//#include "stdafx.h"
#include <iostream>
#include <string>


//#define OS_TYPE_WINDOWS_CC
//#define OS_TYPE_LINUX_CC

#ifdef OS_TYPE_WINDOWS_CC
       #include <Windows.h>
#endif // OS_TYPE_WINDOWS_CC





enum class ConsoleColor
{
	Green, Red, Blue, White, Black,Yellow,Purple,Gray,Cyan,None,
	GreenIntensity, RedIntensity, BlueIntensity, WhiteIntensity, BlackIntensity, YellowIntensity, PurpleIntensity, GrayIntensity,CyanIntensity
};

enum class ConsoleBackgroundColor
{
	Green, Red, Blue, White, Black, Yellow, Purple, Gray, Cyan,None,
	GreenIntensity, RedIntensity, BlueIntensity, WhiteIntensity, BlackIntensity, YellowIntensity, PurpleIntensity, GrayIntensity
};

std::wostream& operator<< (std::wostream& os, ConsoleColor  data)
{
	switch (data)
	{
	case ConsoleColor::Green: {

#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_GREEN);
#else 
		std::wcout << "33[32m";
#endif



	}break;
	case ConsoleColor::Black: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, 0);
#else 
		std::wcout << "33[30m";
#endif

	}break;
	case ConsoleColor::Blue: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE);
#else 
		std::wcout << "33[34m";
#endif

	}break;
	case ConsoleColor::Gray: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
#else 
		std::wcout << "33[37m";
#endif

	}break;
	case ConsoleColor::Purple: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_RED);
#else 
		std::wcout << "33[35m";
#endif

	}break;
	case ConsoleColor::Red: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_RED);
#else 
		std::wcout << "33[31m";
#endif

	}break;
	case ConsoleColor::White: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
#else 
		std::wcout << "33[37m";
#endif

	}break;
	case ConsoleColor::Cyan: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN);
#else 
		std::wcout << "33[36m";
#endif

	}break;
	case ConsoleColor::Yellow: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN);
#else 
		std::wcout << "33[33m";
#endif

	}break;
	case ConsoleColor::None: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN);
#else 
		std::wcout << "33[0m";
#endif

	}break;

	case ConsoleColor::GreenIntensity: {

#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[32m;1m";
#endif



	}break;
	case ConsoleColor::BlackIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, 0);
#else 
		std::wcout << "33[30m;1m";
#endif

	}break;
	case ConsoleColor::BlueIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[34m;1m";
#endif

	}break;
	case ConsoleColor::GrayIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[37m;1m";
#endif

	}break;
	case ConsoleColor::PurpleIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[35m;1m";
#endif

	}break;
	case ConsoleColor::RedIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[31m;1m";
#endif

	}break;
	case ConsoleColor::WhiteIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[37m;1m";
#endif

	}break;
	case ConsoleColor::YellowIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[33m;1m";
#endif

	}break;
	case ConsoleColor::CyanIntensity: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
#else 
		std::wcout << "33[36m;1m";
#endif

	}break;

	default:
		break;
	}

	return os;
}

std::wostream& operator<< (std::wostream& os, ConsoleBackgroundColor  data)
{
	switch (data)
	{
	case ConsoleBackgroundColor::Green: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, BACKGROUND_GREEN);
#else 
		std::wcout << "33[42m";
#endif

	}break;
	case ConsoleBackgroundColor::Black: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, 0);
#else 
		std::wcout << "33[40m";
#endif

	}break;
	case ConsoleBackgroundColor::Blue: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, BACKGROUND_BLUE);
#else 
		std::wcout << "33[44m";
#endif

	}break;
	case ConsoleBackgroundColor::Gray: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, 0);
#else 
		std::wcout << "33[40m";
#endif

	}break;
	case ConsoleBackgroundColor::Purple: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, BACKGROUND_RED | BACKGROUND_BLUE);
#else 
		std::wcout << "33[45m";
#endif

	}break;
	case ConsoleBackgroundColor::Red: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, BACKGROUND_RED);
#else 
		std::wcout << "33[41m";
#endif

	}break;
	case ConsoleBackgroundColor::White: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN);
#else 
		std::wcout << "33[47m";
#endif

	}break;
	case ConsoleBackgroundColor::Yellow: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, BACKGROUND_RED | BACKGROUND_GREEN);
#else 
		std::wcout << "33[43m";
#endif

	}break;
	case ConsoleBackgroundColor::Cyan: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, BACKGROUND_BLUE | BACKGROUND_GREEN);
#else 
		std::wcout << "33[46m";
#endif

	}break;
	case ConsoleBackgroundColor::None: {
#ifdef OS_TYPE_WINDOWS_CC
		HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
		SetConsoleTextAttribute(handle, 0);
#else 
		std::wcout << "33[40m";
#endif

	}break;
	default:
		break;
	}

	return os;
}

测试文件

#define OS_TYPE_WINDOWS_CC
#include "ColorfulConsoleIO.h"
using std::wcout; 
using std::endl;

int main()
{

	std::cout << "what color it is ?" << endl;
	wcout << ConsoleColor::Green << "so what color it is?" << endl;
	wcout << ConsoleColor::Green<< "1abc" << endl;
	wcout << ConsoleColor::Red << "2red red is fire" << endl;
	wcout << ConsoleColor::Blue << "3blue blue love is blue" << endl;
	wcout << ConsoleColor::White << "4blue blue love is blue" << endl;
	wcout << ConsoleColor::Black << "5blue blue love is blue" << endl;
	wcout << ConsoleColor::Purple << "6blue blue love is blue" << endl;
	wcout << ConsoleColor::Yellow << "7blue blue love is blue" << endl;
	wcout << ConsoleColor::Cyan << "8blue blue love is blue" << endl;
	
	wcout << ConsoleColor::GreenIntensity << "1abc" << endl;
	wcout << ConsoleColor::RedIntensity << "2red red is fire" << endl;
	wcout << ConsoleColor::BlueIntensity << "3blue blue love is blue" << endl;
	wcout << ConsoleColor::WhiteIntensity << "4blue blue love is blue" << endl;
	wcout << ConsoleColor::BlackIntensity << "5blue blue love is blue" << endl;
	wcout << ConsoleColor::PurpleIntensity << "6blue blue love is blue" << endl;
	wcout << ConsoleColor::YellowIntensity << "7blue blue love is blue" << endl;
	wcout << ConsoleColor::CyanIntensity << "8blue blue love is blue" << endl;


	wcout << ConsoleBackgroundColor::Green << "1abc" << endl;
	wcout << ConsoleBackgroundColor::Red << "2red red is fire" << endl;
	wcout << ConsoleBackgroundColor::Blue << "3blue blue love is blue" << endl;
	wcout << ConsoleBackgroundColor::Black << "4abc" << endl;
	wcout << ConsoleBackgroundColor::White << "5red red is fire" << endl;
	wcout << ConsoleBackgroundColor::Purple << "6blue blue love is blue" << endl;
	wcout << ConsoleBackgroundColor::Yellow << "7blue blue love is blue" << endl;
	wcout << ConsoleBackgroundColor::Cyan << "8blue blue love is blue" << endl;

	


	wcout << ConsoleBackgroundColor::None;
	wcout << ConsoleColor::None;


	while(1);
    return 0;
}

运行效果

大家可用这个彩色字体开发一个古老的电脑游戏 

最后

以上就是老实白昼为你收集整理的C/C++改变控制台的字体颜色的全部内容,希望文章能够帮你解决C/C++改变控制台的字体颜色所遇到的程序开发问题。

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

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

评论列表共有 0 条评论

立即
投稿
返回
顶部