//following codes once change the colour, then they keep them along all the way, //if i want to write juxt one word with different colour like cout<<"4June" //should be with red jxt the colour of text should change not the background //colour and the if i display something like cout<<"colour not changed"; then //colour should be the original one, how to attain that c++????
#include <iostream>
#include <windows.h>
#include <winnt.h>
#include <stdio.h>
#include "stdafx.h"
using namespace std;
int main(int argc, char* argv[])
{
HANDLE consolehwnd = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "this text is not colorized\n";
SetConsoleTextAttribute(consolehwnd, FOREGROUND_RED);
cout << "this text shows as red\n";
SetConsoleTextAttribute(consolehwnd, FOREGROUND_BLUE);
cout << "this text shows as blue\n";
}
OR
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),1)
OR
system("color 3");
cout<<"colour changed"<<endl;**
if you want
red
text to ablack
background you would just typeSetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4); cout<<"4th June";
To reset back to normal color, set it to color
7
.SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),7);
Here is a table of all the Console colors. you can make 256 combinations by just entering the color code number from the above image to get the desired color.
Below is some console color management code.