C++ Console : Color
It's obvious that in modern Graphical and Colored world of computer, it's kind of disappointment to work in weird looking console with black and white interface. But don't be disappointed about that. It's just the beginning. Software world has endless possibilities. Would you believe that you can even make whole game in console?? Yup, when i was introduced to programming via C++ console apps, i had no idea that it could be this much fun. I even wrote generic codes for some graphical stuff. It's really helpful in learning coding.
Let's see what is there to get colorized... There are 2 ways to get colored. One is to just make it all colored. Like green text with black background:
But the problem with this type of coloring is that it is not very creative. You'll have only one color at a time. And that is the problem. We can't achieve multicolored interface. But on the other hand this is simple way to achieve colored interface. Here is how:
* More details about range of colors are given below.
Let's see what is there to get colorized... There are 2 ways to get colored. One is to just make it all colored. Like green text with black background:
Simple color:
But the problem with this type of coloring is that it is not very creative. You'll have only one color at a time. And that is the problem. We can't achieve multicolored interface. But on the other hand this is simple way to achieve colored interface. Here is how:
#include <iostream>
#include <windows.h>
using namespace std;
void main( void ){
system("color a");
system("pause");
}
I should mention that the command system is used to run Command Prompt commands. Like if you run CMD in windows and type color a and press enter then it'll change the color of all text in it. So if we want to run same command through our program we write system("command"); to send the command to CMD.* More details about range of colors are given below.
And there is more detailed way of doing things like separately color the text. Like this one:
This is very useful and functional way of coloring because it not only gives a way to simple multicolored text but also it is useful in drawing some basic graphics and graphical shapes. We can easily make lines and boxes to represent different things.
The code for multicolor text is some what lengthy then previously discussed but here I'll tell you a way that will help in changing color in one small command like this:
Multicolor:
This is very useful and functional way of coloring because it not only gives a way to simple multicolored text but also it is useful in drawing some basic graphics and graphical shapes. We can easily make lines and boxes to represent different things.
The code for multicolor text is some what lengthy then previously discussed but here I'll tell you a way that will help in changing color in one small command like this:
Color("type");
Type will be a number between 1-15 and it will change the color for any text typed after running this command. If you are not familiar with the functions. you can just add following code before and after the main function of you program and use the above method in program as needed:
#include <windows.h>
using namespace std;
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
void Color(int);
void main( void ) {
//use the Color method here with color codes given below
}
void Color(int color_code) {
SetConsoleTextAttribute(out, y);
}
Just put highlighted code in you program and you can use Color(code); method to color the text in your program.
There are 15 build in colors in console. These are just color combinations in Hex values when we use a color number (code) the program converts it in respective color combination in Hex format and apply that. No need to get in details. Whole idea is that you can also highlight text with values more then 15.
Here are the 15 numbers with their respective colors:
Here is the code that you can run to get this screen for clear view:
#include <iostream> // for printing text
#include <conio.h> // for stopping program in end
#include <Windows.h> // for coloring function
using namespace std;
HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
void Color(int);
void main(){
for (int i = 1; i <= 15; i++)
{
Color(7);
cout << "Color code : " << i << " <====> ";
Color(i);
cout << char(219) << char(219) << endl;
}
_getch();
}
void Color(int y){
SetConsoleTextAttribute(out, y);
}
For the color type command you can use following codes:
Comments
Post a Comment