c 2b 2b change console color

Solutions on MaxInterview for c 2b 2b change console color by the best coders in the world

showing results for - "c 2b 2b change console color"
Henri
05 May 2016
1WORD color = 0x0F; // White
2SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
3cout << "Hello World" << endl;
4SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), color);
5// The farbcodes are the same as they, when you type "color help" in the windows 
6// cmd in. First the number, then the letter (here the output of the cmd):
7/*  0 = Black       8 = Gray
8    1 = Blue        9 = Light Blue
9    2 = Green       A = Light Green
10    3 = Aqua        B = Light Aqua
11    4 = Red         C = Light Red
12    5 = Purple      D = Light Purple
13    6 = Yellow      E = Light Yellow
14    7 = Light Gray  F = White
15    */
16// So, to get the output white again, you have the set the color to:
17// 0x + 0 (Black Background) + F (White Foreground)
18// This works on every OS!
Natalie
29 Jan 2019
1//This is a header file taken from cplusplus.com
2//http://www.cplusplus.com/articles/Eyhv0pDG/
3//concol.h
4#ifndef _INC_EKU_IO_CONCOL
5#define _INC_EKU_IO_CONCOL
6
7/*Header file to color text and background in windows console applications
8Global variables - textcol,backcol,deftextcol,defbackcol,colorprotect*/
9
10#include<windows.h>
11#include<iosfwd>
12
13namespace eku
14{
15
16#ifndef CONCOL
17#define CONCOL
18	enum concol
19	{
20		black=0,
21		dark_blue=1,
22		dark_green=2,
23		dark_aqua,dark_cyan=3,
24		dark_red=4,
25		dark_purple=5,dark_pink=5,dark_magenta=5,
26		dark_yellow=6,
27		dark_white=7,
28		gray=8,
29		blue=9,
30		green=10,
31		aqua=11,cyan=11,
32		red=12,
33		purple=13,pink=13,magenta=13,
34		yellow=14,
35		white=15
36	};
37#endif //CONCOL
38
39	HANDLE std_con_out;
40	//Standard Output Handle
41	bool colorprotect=false;
42	//If colorprotect is true, background and text colors will never be the same
43	concol textcol,backcol,deftextcol,defbackcol;
44	/*textcol - current text color
45	backcol - current back color
46	deftextcol - original text color
47	defbackcol - original back color*/
48
49	inline void update_colors()
50	{
51		CONSOLE_SCREEN_BUFFER_INFO csbi;
52		GetConsoleScreenBufferInfo(std_con_out,&csbi);
53		textcol = concol(csbi.wAttributes & 15);
54		backcol = concol((csbi.wAttributes & 0xf0)>>4);
55	}
56
57	inline void setcolor(concol textcolor,concol backcolor)
58	{
59		if(colorprotect && textcolor==backcolor)return;
60		textcol=textcolor;backcol=backcolor;
61		unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
62		SetConsoleTextAttribute(std_con_out,wAttributes);
63	}
64
65	inline void settextcolor(concol textcolor)
66	{
67		if(colorprotect && textcolor==backcol)return;
68		textcol=textcolor;
69		unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
70		SetConsoleTextAttribute(std_con_out,wAttributes);
71	}
72
73	inline void setbackcolor(concol backcolor)
74	{
75		if(colorprotect && textcol==backcolor)return;
76		backcol=backcolor;
77		unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
78		SetConsoleTextAttribute(std_con_out,wAttributes);
79	}
80
81	inline void concolinit()
82	{
83		std_con_out=GetStdHandle(STD_OUTPUT_HANDLE);
84		update_colors();
85		deftextcol=textcol;defbackcol=backcol;
86	}
87
88	template<class elem,class traits>
89	inline std::basic_ostream<elem,traits>& operator<<(std::basic_ostream<elem,traits>& os,concol col)
90	{os.flush();settextcolor(col);return os;}
91
92	template<class elem,class traits>
93	inline std::basic_istream<elem,traits>& operator>>(std::basic_istream<elem,traits>& is,concol col)
94	{
95		std::basic_ostream<elem,traits>* p=is.tie();
96		if(p!=NULL)p->flush();
97		settextcolor(col);
98		return is;
99	}
100	
101}	//end of namespace eku
102
103#endif	//_INC_EKU_IO_CONCOL
Isaac
05 Nov 2020
1//This is one way to do it. Taken from stackoverflow.
2system("color 70");
3//It just runs a cmd command.
queries leading to this page
windows cmd std 3a 3acout colorc 2b 2b colors in consolec 2b 2b console text colorc 2b 2b console change text color 3cconcepts 3echange only text color in c 2b 2b consolecolors in c 2b 2b consolec 2b 2b print colored text windowsc 2b 2b windows colorconsole color codes c 2b 2bchange color of console text in c 2b 2bchange text color c 2b 2b consolecolors with c 2b 2bc 2b 2b windows set console colorhow to change color in c 2b 2b consolecpp console colourconsole text attribute in c 2b 2bdoes console support color c 2b 2bconsole attributes c 2b 2b colorscolor in c 2b 2b consolec 2b 2b printing in color in windowsdc 2b 2b change text color consoleprint in color windows h ccpp color text cmdwindows colors terminal c 2b 2bcolors in console c 2b 2bc 2b 2b console color codescolors in console using c 2b 2bconsole text color c 2b 2bc 2b 2b console app colourc 2b 2b cmd color textchange console text mode c 2b 2bconsole color in c 2b 2bchange output color in c 2b 2b console cout console color in c 2b 2bconsole text color c 2fc 2b 2btext colour windows console c 2b 2bc 2b 2b change console attrivutesreset console text attribute c 2b 2bc 2b 2b console colorcolors c 2b 2b consolechanging console color c 2b 2bc 2b 2b command line colorc 2b 2b colors consolecpp change console colorcolor in c 2b 2b output windowsc 2b 2b set color consolecpp use colorloop through every system color c 2b 2bc 2b 2b set console text colorset console pink c 2b 2bchange console text color c 2b 2b linuxconsola colors c 2b 2bhow to change color of console in c 2b 2bc 2b 2b console color windowshow to change console background color in c 2b 2brand color terminal cppc 2b 2b system color a1 listchange console color c 2b 2b linuxwindows console text color c 2b 2bhow to change letter color in console c 2b 2bconsole colors c 2b 2bc 2b 2b give color to a text consolec 2b 2b change color consolecpp change console color linuxrun c 2b 2b in cmd but no colorc 2b 2b coloured text in consoleconsole color c 2b 2bc 2b 2b text color consolehow to change text color in c 2b 2b consolec 2b 2b console background colorhow to change output color in c 2b 2b windowschange console color c 2b 2bcolorize screen c 2b 2bchange color of console in c 2b 2bchange console background and text color c 2b 2bc 2b 2b set console background color blackcolor writing c 2b 2b cmdcan i change cmd colors in code c 2b 2bc 2b 2b change console colorbest console color in c 2b 2bc 2b 2b cout color windowswhat coloring does c 2b 2b usec 2b 2b color console texthow to change the console font colour c 2b 2bcolors c 2b 2bc 2b 2b system color codesc 2b 2b console text color multiplatformhow to change the system color of your console in c 2b 2bhow to make text console with windows hc 2b 2b print colored characters on consolehow to use color c 2b 2bc 2b 2b change console color simplec 2b 2b console colour numberssystem 28 22color 1a 22 29 3b all colorsc 2b 2b coloured consolecolor text c 2b 2b consolechange console color text c 2b 2bhow to change background color in c 2b 2b consolecolour full console in c 2b 2bc 2b 2b colored consolec 2b 2b windows color consolec 2b 2b colored output windowshow to return a console color c 2b 2bred text c 2b 2bstd 3a 3acout color windowscolor console c 2b 2bhow to give green color output in cmd cpp codec 2b 2b color consolec 2b 2b console colourscolor in the cmd code c 2b 2bc 2b 2b output color text windowswindows h c print in colorcolor cycle c 2b 2b with whitec 2b 2b color in cmdc 2b 2b consoleatribute colorsc 2b 2b change color of console textc 2b 2b set console background colorhow to change console color in c 2b 2bc 2b 2b color console codesc 2b 2b console color outputsetconsoletextattribute c 2b 2b colorsc 2b 2b custom console colorsc 2b 2b console colourwindows h console color examplesconsole colors in c 2b 2bc 2b 2b console color textconsole colour c 2b 2bcolor default consola c 2b 2bhow to change the color of the console text in c 2b 2bchange color console c 2b 2bchange color of console c 2b 2bc 2b 2b set console colorc 2b 2b console colorscolors in windows hchange console text color c 2b 2bcpp console coolorc 2b 2b console individual colorc 2b 2b white consoleblack color for console output c 2b 2bconsole colors cppadd color to console c 2b 2bconsole color cpphow to change terminal color in c 2b 2bc 2b 2b how to print colored text to consolec 2b 2b colored text powershellwhy my c 2b 2b output in cmd no colorc 2b 2b cout in colour windowsc 2b 2b console change colorhow to change font color in c 2b 2b consolec 2b 2b change std 3a 3acout color windowsset console text c 2b 2b colorscolor default console c 2b 2bcolor c 2b 2b consoleregular colors with c 2b 2bcpp highlight consolehow to change console text color in c 2b 2bcolored c 2b 2b consolecpp color codes system colorc 2b 2b log colorc 2b 2b colored asciihow to write with color to terminal with c 2b 2bset console color in c 2b 2bcolor text for console c 2b 2bchange color c 2b 2b consolehow to change console line color in c 2b 2bchange console colour c 2b 2bdifferent console text colors in c 2b 2bhow to set console output color in c 2b 2bhow to change the console text color in c 2b 2bcpp set console color windowsprint colored text c 2b 2b windowscolor console characters c 2b 2bset console color c 2b 2bcolors in c 2b 2bcpp set console colorshowing colour text in c 2b 2b consoleprint in console with colors c 2b 2bc 2b 2b red colorc 2b 2b console font colorcolor in c 2b 2bc 2b 2b how to change console colorcolor text console c 2b 2bhighlight text console c 2b 2bvisual c 2b 2b console character colorc 2b 2b what is col codehow to add colors to console in c 2b 2bc 2b 2b cout 3c 3c color text windowscolor writing c 2b 2b consolec 2b 2b color terminal windowschange text color in c 2b 2b consolechange color character c 2b 2b consolecolor cppchange terminal color c 2b 2bwindows h console color exerciseswindows console colors c 2b 2bc 2b 2b color text consolec 2b 2b console color changec 2b 2b colorsdefault console text color in c 2b 2bhow to change the color of the console in c 2b 2bhow to change color console in c 2b 2bcolors in the console in c 2b 2badd color ti c 2b 2b command linec 2b 2b change console color