console colors in C++
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//This is one way to do it. Taken from stackoverflow.
system("color 70");
//It just runs a cmd command.
Also in C++:
- Title
- c++ string^ to char*
- Category
- C++
- Title
- c++ uint32_t
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- eratosthenis sieve in c++
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- c++ cli convert string to string^
- Category
- C++
- Title
- primeros numeros primos
- Category
- C++
- Title
- statement that causes a function to end in c++
- Category
- C++
- Title
- c++ functions
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- fmod c++
- Category
- C++
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- rosrun actionlib_msgs genaction.py
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- ue4 c++ struct
- Category
- C++
- Title
- what is time complexity of min_element()
- Category
- C++
- Title
- pointer related problems dangling/wild pointers c++
- Category
- C++
- Title
- c++ read matttrix from text file
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- how to print 5 precision float in c++
- Category
- C++
- Title
- c++ parse int
- Category
- C++
- Title
- how to execute c++ program in cmd
- Category
- C++
- Title
- memset
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- retu7rn this c++
- Category
- C++
- Title
- how to convert string into number
- Category
- C++
- Title
- Create a program that finds the minimum value in these numbers
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- monotonic deque
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- double to int c++
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- inverser les éléments d'un tableau manuellement en c++
- Category
- C++
- Title
- substr c++
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- std string find character c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- c++ map find
- Category
- C++
- Title
- ternary operator c++
- Category
- C++
- Title
- how to input multiple lines of a file in c++
- Category
- C++
- Title
- how to initialize a vector in c++
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- sort function in vector c++ stl
- Category
- C++
- Title
- initialize int c++
- Category
- C++
- Title
- decimal to hex cpp
- Category
- C++
- Title
- access last element in vector in c++
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- in c, is class uppercase or lowercase
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- convert string to stream c++
- Category
- C++
- Title
- c++ allocate dynamic with initial values
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- how to format decimal palces in c++
- Category
- C++
- Title
- array as parameter c++
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- bellman ford algorithm cp algorithm
- Category
- C++
- Title
- write to file in C++
- Category
- C++
- Title
- how to end a c++ program early
- Category
- C++
- Title
- bool function in c++
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- c++ program to input and print text using Dynamic Memory Allocation.loop
- Category
- C++
- Title
- delete a double pointer c++
- Category
- C++
- Title
- c++ movment
- Category
- C++
- Title
- how to print to the serial monitor arduino
- Category
- C++
- Title
- how to import getline in c++
- Category
- C++
- Title
- glfw initialize in c++
- Category
- C++
- Title
- std::substring
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- static_cast c++
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- ios_base::sync_with_stdio(false);cin.tie(NULL);
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- iterar un map c++
- Category
- C++
- Title
- initialization list c++
- Category
- C++
- Title
- how to get a letter from the user c++ string
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- Find a element in a map C++
- Category
- C++
- Title
- cout console
- Category
- C++
- Title
- hashing in competitive programming
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- stack c++
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++