Saturday, November 16
Colour Coding in C++
The first time i saw colour in c++ was amazing experience because just black and white stuff is more like old school comparing to modern software and technology. It was hard to find well defined code and definition for colours in c++ language ; took me a while but i got it finally so here is this code in simple language, hope it helps you...
_________________________________________________________________________
To change whole screen colour i.e. foreground (text) and background, it is very simple and easy command.
The header file for this command is : <windows.h>
The command is : system("Colour _ _");
In place of _ _ you have to enter colour combination you want ; first place takes background colour and 2nd place takes foreground colour (text colour) this is list of all colours available for this command:
1_____Blue A______Light Green
2_____Green B______Light Aqua
3_____Aqua C______Light Red
4_____Red D______Light Purple
5_____Purple E______Light Yellow
6_____Yellow F______Bright White
7_____White
8_____Gray
9_____Light Blue
0_____Black
It's not perfact colouring but it is simple to use
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- system("Color 0A"); // first 0 = black background second A = light green
- cout<<"This is my fev. colour combination for this command"<<endl;
- return 0;
- }
Press any key to continue . . .
=========================================================================
The detailed coding
_________________________________________________________________________
This must be wirten at start of main: HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
The header file for this command is : <windows.h>
The command is : SetConsoleTextAttribute(color, _ );
2_____Green 11______Light Aqua
3_____Aqua 12______Light Red
4_____Red 13______Light Purple
5_____Purple 14______Light Yellow
6_____Yellow 15______Bright White
7_____White
8_____Gray
9_____Light Blue
0_____Black
But the best thing is that it doesnot stops here at 15 ; you can go up to 300 for different colour combinations for headline your text on your program; enjoy
Like this post then i will tell you how to use 2nd one command very easily like this:
clr( _ );
that's it..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
To change minor colour i.e. colour of just 1 character so that we can use as much colour combinations ; it comes to this code because command showed above will not allow you to print 2 or more colours in single output combination. This command is not capable of changing whole background so i prefer to select background colour from system command and then change text colour from this code i'm about to share with you guys.
This must be wirten at start of main: HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE);
The header file for this command is : <windows.h>
The command is : SetConsoleTextAttribute(color, _ );
In place of _ you have to enter colour code you want ; there is a brief list of some basic colours for this command:
1_____Blue 10______Light Green2_____Green 11______Light Aqua
3_____Aqua 12______Light Red
4_____Red 13______Light Purple
5_____Purple 14______Light Yellow
6_____Yellow 15______Bright White
7_____White
8_____Gray
9_____Light Blue
0_____Black
- #include <iostream>
- #include <windows.h>
- using namespace std;
- int main()
- {
- HANDLE color=GetStdHandle(STD_OUTPUT_HANDLE); //just once
- SetConsoleTextAttribute(color, 10);
- cout<<"This";
- SetConsoleTextAttribute(color, 11);
- cout<<" is";
- SetConsoleTextAttribute(color, 12);
- cout<<" a";
- SetConsoleTextAttribute(color, 13);
- cout<<" colour";
- SetConsoleTextAttribute(color, 14);
- cout<<" combination"<<endl;
- return 0;
- }
Press any key to continue . . .
But the best thing is that it doesnot stops here at 15 ; you can go up to 300 for different colour combinations for headline your text on your program; enjoy
Like this post then i will tell you how to use 2nd one command very easily like this:
clr( _ );
that's it..@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
I usually used "CF" of "FC" for color coding in my C++ tasks
ReplyDeletewish i had those programs of first semester with me, those shashkaas :P
ReplyDeleteReally Professional Blogging ,,,, Usman Shery Good One ,, It is Really Helpfull
ReplyDeleteMay ALLAH bless you. Aameen :)
ReplyDeletethanks i appritiate it
ReplyDeletei hope you are finding me useful?