/*
* The simplest way to have colourful text in the console
* Author: Danny Battison
* Contact: gabehabe@hotmail.com
*/
#include <iostream> /* Standard input/output stream */
#include <windows.h> /* Standard Windows API header */
void SetColour (int index) /* Set the colour, just pass the index */
{
/* Pass the console handle and the index (param) to the Win API function */
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), index);
}
void ResetColour () /* Reset the colour to the default (white) */
{
/* Pass 7 as the colour for the original white text colour */
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
/* This following function is just for testing purposes */
void TestColours (int i) /* Test the colours to see which key you want to use */
{
if (i == 255) /* Change this number if you want to loop higher */
return; /* Exit the recursive function if the number has been reached */
else
SetColour (i); /* Set the colour and output it */
std::cout << "colour key: " << i << std::endl;
TestColours (++i); /* Increment i and pass it recursively */
}
int main()
{
std::cout << "This is the original colour for text..." << std::endl;
SetColour (5);
std::cout << "This text should be colourful!" << std::endl;
ResetColour ();
std::cout << "This is the original colour for text again..." << std::endl;
std::cin.get (); /* Pause before testing the colours */
TestColours (0); /* Test the colours with a recursive function */
std::cin.get(); /* Hold the window open */
return EXIT_SUCCESS; /* The program was executed successfully */
}