Colorizing text and console background in C++
Last Updated :
27 Jan, 2021
In C++ programming, the default background of the output screen is black and the text color is the white color, the task is to color both the background and text color in the output screen.
Header File:
The header file required to color the text and background can be either of the given header files:
#include <windows.h>
or
#include <stdlib.h>
Syntax of the color console:
HANDLE console_color;
// Color of the console
console_color = GetStdHandle(STD_OUTPUT_HANDLE);
// P is color code according to your need.
SetConsoleTextAttribute(console_color, P);
Below is the program for the same:
C++
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
HANDLE console_color;
console_color = GetStdHandle(
STD_OUTPUT_HANDLE);
for ( int P = 1; P < 50; P++) {
SetConsoleTextAttribute(
console_color, P);
cout << P << " Hello Geeks, "
<< "good night!!!" ;
}
return 0;
}
|
Output:
Share your thoughts in the comments
Please Login to comment...