Console colors: background problem

Hello people,

I just started to venture into Win32 programming, and I'm, experimenting with console. Here is my code where I achived program that asks user for color code and program then procedes to write in that color.

But background changes. If I type for example "252" then text changes to red but behind text there is line of white (basically whole background is console-black).

Here is my code:
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
#include <iostream>
#include <windows.h>  
#include <string>

using namespace std;

int main () {


	HANDLE hConsole;
int k;
 
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
 
// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
// pick the colorattribute k you want
	cout << "Your color?" << endl;
	cin >> k;
SetConsoleTextAttribute(hConsole, k);
	
	cout << "Hello, this is in color" << endl;
	system ("Pause");
}

	return 0;
}


I wonder how can I change whole color before program types out message (for example I would like background to be and stay black and text goes red).

Thanks in advance!!
If you want the entire console to change colour, you'll have to modify windows itself. This can be done in the registry.

At the start of your program, use this to open the applicable key.
1
2
HKEY hKey;
long nError = RegOpenKeyExA(HKEY_CURRENT_USER,"Console",0,KEY_WRITE,&hKey)


Then use this to set a colour value. The 5th parameter may need to be *colour, I forget.
1
2
DWORD colour = 252;
RegSetValueExA(hKey, "ScreenColors", 0, REG_DWORD, &colour , sizeof(colour)) 


Then you want to close the key.
RegCloseKey(hKey);

Note: Change the values back to "7" when you are done.

Edit: I'm not sure, but this may not take effect until you restart the console. I'm not sure what kind of a workaround you can use, but you may be able to open a new console and delete the old one.
Last edited on
Idea if the work-around is required.

You can create two excecutables. The first would set the registry as above and then call the second exe which contains your program.

You would need to use
CreateProcess( ) and add CREATE_NEW_CONSOLE in the dwCreationFlags parameter.
WHOA, wait a sec. You are not talking about setting the entire background including the stuff before the program excecutes. You are talking about just making red-on-black. This is really easy!

Instead of using SetConsoleTextAttribute(hConsole, 252) use SetConsoleTextAttribute(hConsole,12). Do that at the start of your program.

If you want to make the ENTIRE console background something other than black, you'll proabably need to use the methods I explained above.
Last edited on
Man, im not using SetConsoleTextAttribute(hConsole, 252) I'm using SetConsoleTextAttribute(hConsole,k); because i want USER to choose COLOR of TEXT while BACKGROUND remains black.

Also, it seems that 252 code brings up the red text but in it's own background (foreground?) it's white, how's that?

Thanks!
Last edited on
@Skunk

Setting the SetConsoleTextAttribute(hConsole,k) to 252 is setting the background to white and foreground, red. If you want ONLY a black background, you must let the user only pick number from 0 to 15.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
enum {
	black,          //  0
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};


To choose any of the 16 foreground/background colors, I use int color = (foreground_color+(background_color*16));.
So letting background_color be 0, any of the other numbers as foreground, yields that combo. Setting foreground to 15, makes the background, white, with a foreground of whatever color you chose as the foreground. Hope this was helpful.
Last edited on
Yes it was, thanks!! =)
Topic archived. No new replies allowed.