The code below will demonstrate how to print text with colors in a console.
Note : This will run only in windows.
Code: Select all
/*
major colors are
FOREGROUND_RED,FOREGROUND_BLUE,FOREGROUND_GREEN
BACKGROUND_BLUE,BACKGROUND_GREEN and BACKGROUND_RED
you can create new colors by performing the bit-wise operations on these colors
please note that the MSDOS is not based on RGB color scheme. So it
only supports for 6 colors (as far as I know).
FOREGROUND_INTENSITY - this will increase the intensity of the text in the DOS screen
BACKGROUND_INTENSITY - this will increase the intensity of the background color of the DOS screen
*/
#include <windows.h>
#include <stdio.h>
HANDLE hConsole;
CONSOLE_SCREEN_BUFFER_INFO ScreenBufInfo;
WORD colorWhite =BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED;
//These constants are used to draw the lines
char line[] = {
0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,
0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,
0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,
0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0xDB,0x0A
};
void cPrintf(const char* szValue,WORD color);
int main()
{
cPrintf("This is Red Color\n",FOREGROUND_RED);
cPrintf("This is Blue Color\n",FOREGROUND_BLUE);
cPrintf("This is Green Color\n",FOREGROUND_GREEN);
printf("\n");
cPrintf("This is Red with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_RED);
cPrintf("This is Blue with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_BLUE);
cPrintf("This is Green with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_GREEN);
cPrintf("This is Yellow with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED);
cPrintf("This is Green with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
cPrintf("This is Purple with high intensity\n",FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_RED);
printf("\n");
cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_RED);
cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_BLUE);
cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_GREEN);
cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_RED);
cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
cPrintf(line,FOREGROUND_INTENSITY|FOREGROUND_BLUE|FOREGROUND_RED);
printf("\n");
cPrintf("This is Red Color with Background\n",FOREGROUND_RED|colorWhite);
cPrintf("This is Blue Color with Background\n",FOREGROUND_BLUE|colorWhite);
cPrintf("This is Green Color with Background\n",FOREGROUND_GREEN|colorWhite);
printf("\n\n");
system("pause");
return 0;
}
void cPrintf(const char* szValue,WORD color)
{
// free the memeory for the struct
ZeroMemory(&ScreenBufInfo,sizeof(ScreenBufInfo));
//geting the handle of the console
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// saving the original values of the console buffer
GetConsoleScreenBufferInfo(hConsole,&ScreenBufInfo);
//setting the new color
SetConsoleTextAttribute(hConsole,color);
//printing the value with new color
printf(szValue);
// resting the color to its normal values
SetConsoleTextAttribute(hConsole,ScreenBufInfo.wAttributes);
}