I have been working on a simple program that allows the user to either select a text color, a background color, let the text color be random, or let the background be random. I'm doing well with the text color, as I have used
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Number);
~Number being the number they have inputed.
Now, I want to do background color. How can I set the background color based on an integer variable?
To change the color for just next output, keep using that function.
Notice that if you input the number in hexadecimal format, the first digit would be the background, the second the foreground SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0xe1);
to change the entire console colors use system("Color E1");
(the result of my examples should be yellow background and dark blue text)
Thanks for the help. My question is how I would use variables with this.. When I try and use say, a yellow background with my text color variable, I would have somehting like: system("Color Ecolor");
Well, you could set some const ints to represent the colors, have the user input one, then set it based on comparing the inputted values to const ints.
What I suggest is to merge your variable in a string
1 2 3 4 5 6
string str="Color ";
char* numbers= newchar[2];
int colors = 0xe1;//this is what you change
itoa(colors, numbers, 16);
str+=numbers;
system(str.c_str());
I understand what your doing there bazzy and yes that would work. But i've only used the int to alpha function i think its called? A few times. What does the last value 16 reprisent? 16 bit?
The Win32 Console does not provide blink or alternate charset bits --so both foreground and background fields select the full range of the 16 available colors. <windows.h> includes some #defines for the color names, but their values are all identical to the EGA/VGA values (for each 4-bit color: bit 0 = blue, bit 1 = green, bit 2 = red, bit 3 = intensity).
Hope this helps.
[edit] Oh, I presumed that the default console colors are light gray on black. The user could have changed that in the Console menu --> Properties... . You should use GetConsoleScreenBufferInfo() somewhere in your program's initialization to learn the correct colors, then restore them before your program terminates. [/edit]