// 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>
usingnamespace 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)
}
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.)