Change Background Color

Here is my code so far:
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
// Colored Hello World.cpp : main project file.

#include <stdafx.h> // Used with MS Visual Studio Express. Delete line if using something different
#include <conio.h> // Just for WaitKey() routine
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE); // For use of SetConsoleTextAttribute()

void WaitKey();

int main()
{

	int len = 0,x, y=240; // 240 = white background, black foreground 
	
	string text = "Hello World. I feel pretty today!";
	len = text.length();
	cout << endl << endl << endl << "\t\t"; // start 3 down, 2 tabs, right
	for ( x=0;x<len;x++)
	{
		SetConsoleTextAttribute(console, y); // set color for the next print
		cout << text[x];
		y++; // add 1 to y, for a new color
		if ( y >254) // There are 255 colors. 255 being white on white. Nothing to see. Bypass it
			y=240; // if y > 254, start colors back at white background, black chars
		Sleep(250); // Pause between letters 
	}

	SetConsoleTextAttribute(console, 15); // set color to black background, white chars
	WaitKey(); // Program over, wait for a keypress to close program
}


void WaitKey()
{
	cout  << endl << endl << endl << "\t\t\tPress any key";
	while (_kbhit()) _getch(); // Empty the input buffer
	_getch(); // Wait for a key
	while (_kbhit()) _getch(); // Empty the input buffer (some keys sends two messages)
}

The problem is that it only makes the background of each letter white. I wanted the WHOLE display space background to be white! How could I do this?
Last edited on
I wanted the WHOLE display space background to be white! How could I do this?

You could clear the screen to white?

See "Write a function that will programmatically ..." on this page

How To Performing Clear Screen (CLS) in a Console Application
http://support.microsoft.com/kb/99261

FillConsoleOutputAttribute might be the most intersting call?

Andy
Just the background. I want to be able to display colored letters on the screen still.
hello? anyone?
Have a look at FillConsoleOutputAttribute, as I suggested before.

Together with SetConsoleTextAttribute and GetConsoleScreenBufferInfo, it should do what you require.

See Microsoft example for how these calls are used; the cls function (a) sets all chars to spaces, and (b) sets the text attributes (which includes text and background color.) So you only need half of it.

To start with you could try just setting the color, clearing the console, and then printing some text. When you go onto to changing the text color, you've got to avoid overwriting the background color when you change the text (or foreground) color.

(The attribute set by SetConsoleTextAttribute includes 4 bits for the foreground color, 4 for the text color, and 8 other bits.)

See MSDN for more info, e.g.

Console Screen Buffers
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes

Andy
Last edited on
Topic archived. No new replies allowed.