Coloring the Console Text

Feb 20, 2012 at 5:18am
So I spent about 15 minutes trying to figure out how to code the console text different colors at different times (I am making a text-based game) and I came up with this to make it work:

1
2
3
4
5
6
7
8
9
10
11
12
#include <Windows.h>	// windows API

HANDLE hFontColor = GetStdHandle(STD_OUTPUT_HANDLE);

#define RED 12

void main()
{
iFontColor = RED;
	SetConsoleTextAttribute (hFontColor, iFontColor);
	std::cout << "Welcome to a Basic Text Adventure demonstration using minimal knowledge of C++!"<< std::endl;
}


This is of course just a snipit, I just would like someone to tell me how the Handle, getstdhandle, stdoutputhandle, setconsole text attribute etc. work! Please reply quickly!
Feb 20, 2012 at 5:44am
Maybe google GetStdHandle and SetConsoleTextAttribute instead of asking someone to do it for you.

After that, ask about what you don't understand.
Feb 20, 2012 at 6:15am
closed account (o1vk4iN6)
At first glance I'd say you should put GetStdHandle() into the main function rather than what you're doing now.
Feb 20, 2012 at 10:44am
Welcome to a Basic Text Adventure demonstration using minimal knowledge of C++!


This made me giggle. Minimal knowledge of C++ will tell you that of the 9 effective lines, there's something wrong with 4 of them. That's roughly 45%...
Feb 20, 2012 at 10:10pm
You could always try ANSI escape sequences, works for me on Linux.
And you can always put "using namespace std;" at the top of your code unless you have librarys that conflict with the STL
 
std::cout << "\e[31mRed\e[0m";
Feb 20, 2012 at 10:20pm
Maybe google GetStdHandle and SetConsoleTextAttribute instead of asking someone to do it for you.

After that, ask about what you don't understand.


I Google'd it and it did not make much sense to me.


At first glance I'd say you should put GetStdHandle() into the main function rather than what you're doing now.


I'm using it in several functions, and I am setting it up outside of main() so that I can do so without setting it up every time.


This made me giggle. Minimal knowledge of C++ will tell you that of the 9 effective lines, there's something wrong with 4 of them. That's roughly 45%...


This doesn't seem like a very constructive response to me... the program runs perfectly fine, in fact I started a new one just today that runs better.


Everyone, what I would like to know is what each of the codes mean, I Google'd them, YouTube'd them, etc., and came up without a good explanation.

What is:

GetStdHandle(STD_OUTPUT_HANDLE)

Doing?

Now, though, I realise that in the code:

SetConsoleTextAttribute (hFontColor, iFontColor);

iFontColor is the value 1-256 of the font's coloring, FG + BG * 16 <= 256. What I don't understand, however, is what

GetStdHandle(STD_OUTPUT_HANDLE)

is doing.
Feb 20, 2012 at 11:35pm
closed account (o1vk4iN6)
I'm using it in several functions, and I am setting it up outside of main() so that I can do so without setting it up every time.


That's no excuse for using the function in global namespace, that function is probably being called before the console window is created, which would mean the handle is invalid. You can declare the variable in the global namespace than assign a value to it in main.

1
2
3
4
5
6
HANDLE hFontColor;

int main()
{
     hFontColor = GetStdHandle(STD_OUTPUT_HANDLE);
}
Feb 20, 2012 at 11:42pm
That's no excuse for using the function in global namespace, that function is probably being called before the console window is created, which would mean the handle is invalid. You can declare the variable in the global namespace than assign a value to it in main.


Should I never assign values in the global namespace?
Feb 20, 2012 at 11:59pm
closed account (o1vk4iN6)
There's a time and place for everything, anyways I ran your code and it worked so it might be a configuration problem on your end.
Last edited on Feb 21, 2012 at 12:00am
Feb 21, 2012 at 5:14am
There's a time and place for everything, anyways I ran your code and it worked so it might be a configuration problem on your end.


It works just fine, I just want to know what GetStdHandle(STD_OUTPUT_HANDLE); does in plain English.
Feb 21, 2012 at 9:33am
Retrieves a handle to the specified standard device (standard input, standard output, or standard error).


First result googling. In plain English. Oh my.

If you need some sort of mapping to a standard C++ context, input, output and error handles correspond to std::cin, std::cout and std::cerr.

Topic archived. No new replies allowed.