Changing the Background Color in a program.

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?
Last edited on
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)
Last edited on
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");

How would I do this? Especially with 2 variables?
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= new char[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?
Just write some little functions to keep track of that stuff for you:
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
static int current_text_colors = 07;  // presumably...

int gettextcolor()
  {
  return current_text_colors & 0x0F;
  }

int gettextbackground()
  {
  return (current_text_colors >> 4) & 0x0F;
  }

void settextcolor( int color )
  {
  current_text_colors = (current_text_colors & 0xF0) | (color & 0x0F);
  SetConsoleTextAttribute(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    current_text_colors
    );
  }

void settextbackground( int color )
  {
  current_text_colors = (current_text_colors & 0x0F) | ((color & 0x0F) << 4);
  SetConsoleTextAttribute(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    current_text_colors
    );
  }

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]
Last edited on
@ Mythios
16 is the numeric base on which the integer is saved to the string
( http://www.cplusplus.com/reference/clibrary/cstdlib/itoa.html )
Last edited on
Topic archived. No new replies allowed.