Modify Windows Console Color using C++

Using function SetConsoleTextAttribute() to modify。

Following are the color code and output sample image。

 

 

// http://www.cplusplus.com/forum/beginner/54360/
#include <stdio.h>
#include "conio.h"
#include <iostream>
#include <windows.h>

using namespace std;

/*
The different color codes are

0   BLACK
1   BLUE
2   GREEN
3   CYAN
4   RED
5   MAGENTA
6   BROWN
7   LIGHTGRAY
8   DARKGRAY
9   LIGHTBLUE
10  LIGHTGREEN
11  LIGHTCYAN
12  LIGHTRED
13  LIGHTMAGENTA
14  YELLOW
15  WHITE
*/

enum COLORS
{
	TEXT_BLACK=0x00, TEXT_BLUE=0x01, TEXT_GREEN=0x02, TEXT_CYAN=0x03,
	TEXT_RED=0x04, TEXT_MAGENTA=0x05, TEXT_BROWN=0x06, TEXT_LIGHTGRAY=0x07,
	TEXT_DARKGRAY=0x08, TEXT_LIGHTBLUE=0x09, TEXT_LIGHTGREEN=0x0A, TEXT_LIGHTCYAN=0x0B,
	TEXT_LIGHTRED=0x0C, TEXT_LIGHTMAGENTA=0x0D, TEXT_YELLOW=0x0E, TEXT_WHITE=0x0F,

	BACK_BLACK=0x00, BACK_BLUE=0x10, BACK_GREEN=0x20, BACK_CYAN=0x30,
	BACK_RED=0x40, BACK_MAGENTA=0x50, BACK_BROWN=0x60, BACK_LIGHTGRAY=0x70,
	BACK_DARKGRAY=0x80, BACK_LIGHTBLUE=0x90, BACK_LIGHTGREEN=0xA0, BACK_LIGHTCYAN=0xB0,
	BACK_LIGHTRED=0xC0, BACK_LIGHTMAGENTA=0xD0, BACK_YELLOW=0xE0, BACK_WHITE=0xF0,
};

int main ( void )
{
	HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
	WORD wOldColorAttrs;
	CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

	/*
	* First save the current color information
	*/
	GetConsoleScreenBufferInfo(h, &csbiInfo);
	wOldColorAttrs = csbiInfo.wAttributes;
	
	printf("consle color: http://www.cplusplus.com/forum/beginner/54360/ \n\n");
	/*
	* Set the new color information
	*/
	SetConsoleTextAttribute ( h, TEXT_BLACK | BACK_WHITE);
	printf ( "TEXT_BLACK: 0x%X\n", TEXT_BLACK | BACK_WHITE);
	SetConsoleTextAttribute ( h, TEXT_BLUE);
	printf ( "TEXT_BLUE: 0x%X\n", TEXT_BLUE);
	SetConsoleTextAttribute ( h, TEXT_GREEN);
	printf ( "TEXT_GREEN: 0x%X\n", TEXT_GREEN);
	SetConsoleTextAttribute ( h, TEXT_CYAN);
	printf ( "TEXT_CYAN: 0x%X\n", TEXT_CYAN);
	SetConsoleTextAttribute ( h, TEXT_RED);
	printf ( "TEXT_RED: 0x%X\n", TEXT_RED);
	SetConsoleTextAttribute ( h, TEXT_MAGENTA);
	printf ( "TEXT_MAGENTA: 0x%X\n", TEXT_MAGENTA);
	SetConsoleTextAttribute ( h, TEXT_BROWN);
	printf ( "TEXT_BROWN: 0x%X\n", TEXT_BROWN);
	SetConsoleTextAttribute ( h, TEXT_LIGHTGRAY);
	printf ( "TEXT_LIGHTGRAY: 0x%X\n", TEXT_LIGHTGRAY);
	SetConsoleTextAttribute ( h, TEXT_DARKGRAY);
	printf ( "TEXT_DARKGRAY: 0x%X\n", TEXT_DARKGRAY);
	SetConsoleTextAttribute ( h, TEXT_LIGHTBLUE);
	printf ( "TEXT_LIGHTBLUE: 0x%X\n", TEXT_LIGHTBLUE);
	SetConsoleTextAttribute ( h, TEXT_LIGHTGREEN);
	printf ( "TEXT_LIGHTGREEN: 0x%X\n", TEXT_LIGHTGREEN);
	SetConsoleTextAttribute ( h, TEXT_LIGHTCYAN);
	printf ( "TEXT_LIGHTCYAN: 0x%X\n", TEXT_LIGHTCYAN);
	SetConsoleTextAttribute ( h, TEXT_LIGHTRED);
	printf ( "TEXT_LIGHTRED: 0x%X\n", TEXT_LIGHTRED);
	SetConsoleTextAttribute ( h, TEXT_LIGHTMAGENTA);
	printf ( "TEXT_LIGHTMAGENTA: 0x%X\n", TEXT_LIGHTMAGENTA);
	SetConsoleTextAttribute ( h, TEXT_YELLOW);
	printf ( "TEXT_YELLOW: 0x%X\n", TEXT_YELLOW);
	SetConsoleTextAttribute ( h, TEXT_WHITE);
	printf ( "TEXT_WHITE: 0x%X\n", TEXT_WHITE);

	std::cout << std::endl;

	SetConsoleTextAttribute ( h, BACK_BLACK | TEXT_WHITE);
	printf ( "BACK_BLACK: 0x%X\n", BACK_BLACK | TEXT_WHITE);
	SetConsoleTextAttribute ( h, BACK_BLUE);
	printf ( "BACK_BLUE: 0x%X\n", BACK_BLUE);
	SetConsoleTextAttribute ( h, BACK_GREEN);
	printf ( "BACK_GREEN: 0x%X\n", BACK_GREEN);
	SetConsoleTextAttribute ( h, BACK_CYAN);
	printf ( "BACK_CYAN: 0x%X\n", BACK_CYAN);
	SetConsoleTextAttribute ( h, BACK_RED);
	printf ( "BACK_RED: 0x%X\n", BACK_RED);
	SetConsoleTextAttribute ( h, BACK_MAGENTA);
	printf ( "BACK_MAGENTA: 0x%X\n", BACK_MAGENTA);
	SetConsoleTextAttribute ( h, BACK_BROWN);
	printf ( "BACK_BROWN: 0x%X\n", BACK_BROWN);
	SetConsoleTextAttribute ( h, BACK_LIGHTGRAY);
	printf ( "BACK_LIGHTGRAY: 0x%X\n", BACK_LIGHTGRAY);
	SetConsoleTextAttribute ( h, BACK_DARKGRAY);
	printf ( "BACK_DARKGRAY: 0x%X\n", BACK_DARKGRAY);
	SetConsoleTextAttribute ( h, BACK_LIGHTBLUE);
	printf ( "BACK_LIGHTBLUE: 0x%X\n", BACK_LIGHTBLUE);
	SetConsoleTextAttribute ( h, BACK_LIGHTGREEN);
	printf ( "BACK_LIGHTGREEN: 0x%X\n", BACK_LIGHTGREEN);
	SetConsoleTextAttribute ( h, BACK_LIGHTCYAN);
	printf ( "BACK_LIGHTCYAN: 0x%X\n", BACK_LIGHTCYAN);
	SetConsoleTextAttribute ( h, BACK_LIGHTRED);
	printf ( "BACK_LIGHTRED: 0x%X\n", BACK_LIGHTRED);
	SetConsoleTextAttribute ( h, BACK_LIGHTMAGENTA);
	printf ( "BACK_LIGHTMAGENTA: 0x%X\n", BACK_LIGHTMAGENTA);
	SetConsoleTextAttribute ( h, BACK_YELLOW);
	printf ( "BACK_YELLOW: 0x%X\n", BACK_YELLOW);
	SetConsoleTextAttribute ( h, BACK_WHITE);
	printf ( "BACK_WHITE: 0x%X\n", BACK_WHITE);
	/*
	* Restore the original colors
	*/
	SetConsoleTextAttribute ( h, wOldColorAttrs);

	system("pause");
	return 0;
}

console_color_code

Reference

2 comments

  1. Thank you for this. Very useful. I have a question regarding background colors. The background color codes you’ve provided only make the space around the text change color. Is there any way to make the background color of the entire output screen change?

Add a Comment

Your email address will not be published. Required fields are marked *

1 × four =