"c++ console color" Code Answer's
You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "c++ console color" answered properly. Developers are finding an appropriate answer about c++ console color related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like c++ console color. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on c++ console color.
c++ change console color
WORD color = 0x0F; // White
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
cout << "Hello World" << endl;
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), color);
// The farbcodes are the same as they, when you type "color help" in the windows
// cmd in. First the number, then the letter (here the output of the cmd):
/* 0 = Black 8 = Gray
1 = Blue 9 = Light Blue
2 = Green A = Light Green
3 = Aqua B = Light Aqua
4 = Red C = Light Red
5 = Purple D = Light Purple
6 = Yellow E = Light Yellow
7 = Light Gray F = White
*/
// So, to get the output white again, you have the set the color to:
// 0x + 0 (Black Background) + F (White Foreground)
// This works on every OS!
c++ console color
SetConsoleTextAttribute(console, ((int)background * 16) + (int)forground);
console colors in C++
//This is a header file taken from cplusplus.com
//http://www.cplusplus.com/articles/Eyhv0pDG/
//concol.h
#ifndef _INC_EKU_IO_CONCOL
#define _INC_EKU_IO_CONCOL
/*Header file to color text and background in windows console applications
Global variables - textcol,backcol,deftextcol,defbackcol,colorprotect*/
#include<windows.h>
#include<iosfwd>
namespace eku
{
#ifndef CONCOL
#define CONCOL
enum concol
{
black=0,
dark_blue=1,
dark_green=2,
dark_aqua,dark_cyan=3,
dark_red=4,
dark_purple=5,dark_pink=5,dark_magenta=5,
dark_yellow=6,
dark_white=7,
gray=8,
blue=9,
green=10,
aqua=11,cyan=11,
red=12,
purple=13,pink=13,magenta=13,
yellow=14,
white=15
};
#endif //CONCOL
HANDLE std_con_out;
//Standard Output Handle
bool colorprotect=false;
//If colorprotect is true, background and text colors will never be the same
concol textcol,backcol,deftextcol,defbackcol;
/*textcol - current text color
backcol - current back color
deftextcol - original text color
defbackcol - original back color*/
inline void update_colors()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(std_con_out,&csbi);
textcol = concol(csbi.wAttributes & 15);
backcol = concol((csbi.wAttributes & 0xf0)>>4);
}
inline void setcolor(concol textcolor,concol backcolor)
{
if(colorprotect && textcolor==backcolor)return;
textcol=textcolor;backcol=backcolor;
unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
SetConsoleTextAttribute(std_con_out,wAttributes);
}
inline void settextcolor(concol textcolor)
{
if(colorprotect && textcolor==backcol)return;
textcol=textcolor;
unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
SetConsoleTextAttribute(std_con_out,wAttributes);
}
inline void setbackcolor(concol backcolor)
{
if(colorprotect && textcol==backcolor)return;
backcol=backcolor;
unsigned short wAttributes=((unsigned int)backcol<<4) | (unsigned int)textcol;
SetConsoleTextAttribute(std_con_out,wAttributes);
}
inline void concolinit()
{
std_con_out=GetStdHandle(STD_OUTPUT_HANDLE);
update_colors();
deftextcol=textcol;defbackcol=backcol;
}
template<class elem,class traits>
inline std::basic_ostream<elem,traits>& operator<<(std::basic_ostream<elem,traits>& os,concol col)
{os.flush();settextcolor(col);return os;}
template<class elem,class traits>
inline std::basic_istream<elem,traits>& operator>>(std::basic_istream<elem,traits>& is,concol col)
{
std::basic_ostream<elem,traits>* p=is.tie();
if(p!=NULL)p->flush();
settextcolor(col);
return is;
}
} //end of namespace eku
#endif //_INC_EKU_IO_CONCOL
console colors in C++
//This is one way to do it. Taken from stackoverflow.
system("color 70");
//It just runs a cmd command.
All those coders who are working on the C++ based application and are stuck on c++ console color can get a collection of related answers to their query. Programmers need to enter their query on c++ console color related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about c++ console color for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of c++ console color while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "c++ console color". Visit this developer's friendly online web community, CodeProZone, and get your queries like c++ console color resolved professionally and stay updated to the latest C++ updates.