Background color

Is there any way to change the background color on a "console application" ?
Through the standard, no. The screen doesn't even exist in C++, so neither do colors. Purely text and a standard input and output, and they're not even defined as to what it has to be.

However, there is several possibilities. If you're on windows, you can call the batch command "color" using the system function. This will effectively change the background and foreground for the console until it's closed or changed again.

You can use an API specific version, there is some code here and on other sites that allow you to change color per character on the console. I have one that works with Windows, other operating systems not so much.

There is the option of going the ANSI route. Again, not standard, and not even guaranteed to work on all three of the main OS's. It is, however, going to work on the widest variety of computers, but isn't guaranteed to work on any.

It's up to you to pick what you want, and it's up to you to decide if you really want to implement it into your code. Again, it's not standard, and consoles aren't meant to use colors, however, some people swear it's what they want/need.
I already know that but i would like to make it general for the program , and not making it getting modified by it's own user
Why don't you just write a GUI App the LOOKS like the console?
If you use the Windows API, you can control each "character block" 's background and foreground color.
@Private

Take a look at my answer to Prashant Gupta PG, here.

http://v2.cplusplus.com/forum/general/88401/
closed account (18hRX9L8)
system("COLOR [attr]");

Sets the default console foreground and background colors.

COLOR [attr]

attr Specifies color attribute of console output

Color attributes are specified by TWO hex digits -- the first
corresponds to the background; the second to the foreground. Each digit
can be any of the following values:

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 = White
F = BringWhite

If no argument is given, this command restores the color to what it was
when CMD.EXE started. This value either comes from the current console
window, the /T command line switch or from the DefaultColor registry
value.

The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
the COLOR command with a foreground and background color that are the
same.

Example: "COLOR fc" produces light red on bright white.
Last edited on
usandfriends is right on. Try

system("color 97"); // blue background white text.

Last edited on
Private wrote:
I already know that but i would like to make it general for the program , and not making it getting modified by it's own user

What?

Code you enter isn't modifiable by the user unless you allow it to be. I don't suggest using the system function since it allows unexpected vulnerabilities in your program. You can read more here: http://cplusplus.com/articles/j3wTURfi/

I suggest either going the OS specific route as that ensures that it works on every version of the program for that OS, or going the route of a third party library. Ansi is also another option, but still has the flaws I stated above. It's easy to implement, but not guaranteed. Here are some examples, excluding the system call that's already been used:
1
2
3
4
5
6
7
8
9
// Windows Specific
   void setconsolecolor(int textColor, int bgColor) {
      SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (textColor + (bgColor * 16)));
   }

// Use
// setconsolecolor(0, 7);
// Sets the text to black and the background to grey I believe
// The valid range is from 0 - 15 


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
46
47
48
49
50
51
52
// Ansi Version
   enum colors { BLACK = 0, RED, GREEN, YELLOW, BLUE, PURPLE, CYAN, GREY,
                 LIGHTGREY, LIGHTRED, LIGHTGREEN, LIGHTYELLOW, LIGHTBLUE,
                 LIGHTPURPLE, LIGHTCYAN, WHITE, DEFAULT };

   void consolecolor(colors textColor = DEFAULT, colors backgroundColor = DEFAULT) {

      // Set default foreground color
      if (textColor == DEFAULT)
         std::cout << "\x1B[39m";

      // Set bright foreground color
      else if (textColor > GREY) {
         // Set bright mode
         std::cout << "\x1B[1m";
         // Set color
         std::cout << "\x1B[3" << textColor - LIGHTGREY << "m";
      }

      // Set normal foreground color
      else {
         // Set normal mode
         std::cout << "\x1B[22m";
         // Set color
         std::cout << "\x1B[3" << textColor << "m";
      }

      // Set default background color
      if (backgroundColor == DEFAULT)
         std::cout << "\x1B[49m";

      // Set bright background color
      else if (backgroundColor > GREY) {
         // Set bright mode
         std::cout << "\x1B[1m";
         // Set color
         std::cout << "\x1B[4" << backgroundColor - LIGHTGREY << "m";
      }

      // Set normal background color
      else {
         // Set normal mode
         std::cout << "\x1B[22m";
         // Set color
         std::cout << "\x1B[4" << backgroundColor << "m";
      }
   }

// Use
// setconsolecolor(BLACK, GREY);
// Same result as above
// Can be any value in enum, otherwise it just returns false without doing anything 


For more about the Ansi options, you can read my post here: http://www.cplusplus.com/forum/lounge/78225/

Please note, there is a small discussion on that thread about why Ansi isn't suggested, but it's something different to mess around with.
Topic archived. No new replies allowed.