Saturday, February 14, 2009

Console: Setting the text color (C++)

Introduction
When making console apps, be it just learning C++ or for a school project, you want your apps to appeal to the feature-hungry crowd.

One way you can increase the loveliness of your application is by easily setting the color your console font.

In all fairness, it's very easy. In order to change the color, we will need to use SetConsoleTextAttribute(), which is located in windows.h.

Code
enum Colors { blue = 1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite };

HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);

void Print(int color, std::string output)
{
SetConsoleTextAttribute( handle, color);
cout << output;
}


2 comments:

fatma said...

hello, I just want to say that when I get the address of this blog, I thought that I will find a good tutorial about C++ but !!!

CppBuilder2006 said...

This was very helpful.
I like colored texts!
good luck

Post a Comment

 

C++ Universe - The universe compromised of C++ and C#. Powered By Blogger © 2009.