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).
#include <iostream>
#include <windows.h>
#include <string>
usingnamespace 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).
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.
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.
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?
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.
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.