C++ Change CMD color background and text
Aug 24, 2011 at 7:47am UTC
Im coding with c++ (dev-c++).
How do i change the background color of my cmd box? But more importantly, how do i change the color of certain text?
Aug 24, 2011 at 7:52am UTC
Check this out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <windows.h>
#include <iostream>
struct Color
{
int color;
Color(int color_): color(color_) {}
Color operator + (const Color & other) const { return Color(this ->color | other.color); }
};
#define FORE_LIGHT(color) const Color cfl##color##_ = FOREGROUND_##color | FOREGROUND_INTENSITY;
#define BACK_LIGHT(color) const Color cbl##color##_ = BACKGROUND_##color | BACKGROUND_INTENSITY;
#define FORE_DARK(color) const Color cfd##color##_ = FOREGROUND_##color;
#define BACK_DARK(color) const Color cbd##color##_ = BACKGROUND_##color;
FORE_LIGHT(RED) FORE_LIGHT(GREEN) FORE_LIGHT(BLUE)
BACK_LIGHT(RED) BACK_LIGHT(GREEN) BACK_LIGHT(BLUE)
FORE_DARK (RED) FORE_DARK (GREEN) FORE_DARK (BLUE)
BACK_DARK (RED) BACK_DARK (GREEN) BACK_DARK (BLUE)
const Color cdefault_ = cfdRED_ + cfdGREEN_ + cfdBLUE_;
std::ostream & operator << (std::ostream & os, Color c)
{
return SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c.color), os;
}
int main()
{
using std::cout;
cout << "Hello, " << cflRED_ << "Red " << cdefault_ << " World!\n" ;
cout << "Hello, " << cflGREEN_ << "Green " << cdefault_ << " World!\n" ;
cout << "Hello, " << cflBLUE_ << "Blue " << cdefault_ << " World!\n" ;
cout << "Hello, " << cflRED_ + cflGREEN_ << "Yellow" << cdefault_ << " World!\n" ;
cout << "Hello, " << cflRED_ + cflBLUE_ << "Pink " << cdefault_ << " World!\n" ;
cout << "Hello, " << cflGREEN_ + cflBLUE_ << "Cyan " << cdefault_ << " World!\n" ;
cout << "Hello, " << cblRED_ + cblGREEN_ + cblBLUE_ << "Black " << cdefault_ << " World!\n" ;
cout << cflRED_ + cflGREEN_ + cflBLUE_ << "\n(hit enter to quit...)" << cdefault_;
std::cin.get();
}
Useful link ->
http://msdn.microsoft.com/en-us/library/ms682073(v=vs.85).aspx
Aug 24, 2011 at 11:16am UTC
return SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c.color), os;
i don't understand this line,how can you return 2 values?
Aug 24, 2011 at 6:27pm UTC
I don't return two values :P I only return
os . What I wrote is another way to write...
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c.color); return os;
Useful link ->
http://en.wikipedia.org/wiki/Comma_operator
Last edited on Aug 24, 2011 at 7:52pm UTC
Aug 25, 2011 at 3:36am UTC
so it has the same effect like when you write it with bracket? that strange, in my opinion, you should write it like this:
return (SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c.color), os);
because, in my opinion once more, the effect of writing like this:
return SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c.color), os;
then the result should be like this:
return 1, os /*whatever the variable contains*/ ;
from wikipedia
i = a, b; // stores a into i. Equivalent to (i = a), b;
how could that possibly happened?
Aug 25, 2011 at 8:10am UTC
The example from wikipedia is different. You also have an assignment operator there, which
has higher precedence than the comma operator. In my example, the only operator is the
comma operator. Therefore, the parentheses are not necessary. Here's another example:
1 2 3 4 5
#include <iostream>
int f() { return 1 + 1, 2 + 2, 3 + 3; }
int main() { std::cout << f() << std::endl; }
Topic archived. No new replies allowed.