Background Colour

How do you change the background colour??
the background of what?
search the web (google is your friend :) and/or these forums ;)

first few hits:

http://www.cplusplus.com/forum/beginner/1640/
http://www.cplusplus.com/forum/beginner/8835/

just search and you'll find..
Last edited on
Rofl..

Garunas, you need to be much much much more descriptive in your questions.
Do you want to change the background color of the console, of a win32 window, of a windows forms app, of a opengl window, of a <insert library here> window, of a picture, of your desktop, of your screen saver.... i could go on.

Description is necessary.
The colour behing the writing:

#include <iostream>

using namespace std;

int main()
{
cout<<"Why is the color behind this writing black? How do I change it?"<<endl;
cout<<"lol";
return 0;
}
sorry I think thats why my posts are in the beginners section haha
search: change bg color c++ on google...

http://www.cplusplus.com/forum/beginner/5618/
http://www.cplusplus.com/forum/general/6147/
http://www.dreamincode.net/forums/topic/15094-how-to-change-color-in-c-background/

i haven't read them, but the answer should be in there...

Search before asking, it's been asked a lot of times :)
The real answer is you probably shouldn't.
I think I'm going to be hated for this but... The simplest way is to have the OS do it for you, in a cmd shell that command is "Color" as seen here:

http://ss64.com/nt/color.html

There is nothing in the iostream library that will change the color of your consule window which I'm only pointing out because it was the only one you included in your sample code.
The simplest way is to have the OS do it for you

*slaps* ... If i didn't, Disch would.

There is nothing in the iostream library that will change the color of your consule window

Yes, but there's functions that control the console itself.
http://msdn.microsoft.com/en-us/library/ms682073%28v=VS.85%29.aspx
SetConsoleTextAttribute probably does what you're looking for. Read some documentation, it all comes together eventually.
In my defence the OP did specify a console window, and was not including any of the MS libraries in his sample code. I never advocated calling system() or anything like that, I left the method to pass the argument completely up to the OP.
Last edited on
I never advocated calling system()

Letting the OS do it for you implies that.
And even if you don't make a call to system(), if you let the OS do it for you, it makes your program platform dependent. You should always try to make your programs able to run on more than Windows, its good practice.

I'm confused on what you're referring to by "OP".
OP means "Original Post" or "Original Poster".

You can use NCurses to set text colour; but if you use NCurses for that you really have to use it for everything. It is cross-platform, though.

If you really need coloured output, go with NCurses.

Edit: If you need me to elaborate on anything, just ask.
Last edited on
A very simple thing to do is to use
system("color 01");

Change the 01 with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
00 = Black
01 = Blue
02 = Green
03 = Aqua
04 = Red
05 = Purple
06 = Yellow
07 = White
08 = Gray
09 = Light Blue
0A = Light Green
0B = Light Aqua
0C = Light Red
0D = Light Purple
0E = Light Yellow
0F = Bright White


This will change all the text in the console into what you choose.
Now from what i read about the command system(); many people say it is wrong from different reasons..one thing i can tell you sure about system("color 01") is that is a bit slow.but it works for fun :)
A very simple thing to do is to use
system("color 01");

http://cplusplus.com/forum/beginner/1988/
Since I know that Mihay07 isn't going to read a 7 page thread I'll sum it up for him. The people on this site who do the most to help others, the go-to-guys as it were, have a thing against using the system() function for several reasons that they explain rather well. This has bled it's way into the culture of this site and will see that you are persecuted and burned as a heretic for suggesting it especially to new users. The only time it seems to be exceptable is when the system() call is a small part of the program and is being used to prototype a solution until the real problem is addressed.
Last edited on
You phrase that rather brutally. No one's getting "persecuted and burned", rather they are being redirected to a thread explaining why calls to system() shouldn't be made, nothing rude involved. People aren't labeled as "noobs" (or other unfriendly terms) just because they recommend using the system() function. They were simply taught a correct (but inefficient, and terribly heavy) way to handle the problem at hand. This isn't their fault, and if it takes redirecting every new person to that same thread to improve peoples code, then so be it.
Well " Computergeek01 " to be honest with you i read something about system() from different links i found and i found in one topic something weary interesting about it.not just stopping the console closing or changing colors.


"when you directly execute a program, it gets the same privileges as your program -- meaning that if, for example, you are running as system administrator then the malicious program you just inadvertently executed is also running as system administrator. If that doesn't scare you silly, check your pulse.
It doesn't matter if you aren't sysadmin either. Anything you can do it can do."


For those who don't now yet,i recommend it.

http://www.cplusplus.com/forum/articles/11153/
Last edited on
Topic archived. No new replies allowed.