
That's simple, but maybe you don't know! So, today i'll show you how to change console text background or text color in VC++
And the codes is here:
#include "stdafx.h"
#include "conio.h"
#include "stdio.h"
#include "windows.h"
void main(int argc, char* argv[])
{
HANDLE hConsole;
int k;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Change color
// Color rang form 0 - 255
for(k = 1; k < 255; k++)
{
SetConsoleTextAttribute(hConsole, k);
printf(" Hello World ");
}
getch();
}
As you can see SetConsoleTextAttribute() function use to change text color.
Other way use to define color first then call it in function.
enum COLORS {
BLACK = 0,
DARK_BLUE = 1,
DARK_GREEN = 2,
TEAL = 3,
DARK_RED = 4,
DARK_PURPLE = 5,
GOLD = 6,
GREY = 7,
DARK_WHITE = 8,
BLUE = 9,
GREEN = 10,
CYAN = 11,
RED = 12,
PURPLE = 13,
YELLOW = 14,
WHITE = 15
};
void SetColor(const int foreground) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, foreground);
return;
}
//Exam to call this function
//SetColor(GREEN);
//SetColor(WHITE);
//SetColor(BLACK);
Search Keywords:
Programming
,
VC++