Coloring the Console Text

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!
Maybe google GetStdHandle and SetConsoleTextAttribute instead of asking someone to do it for you.

After that, ask about what you don't understand.
closed account (o1vk4iN6)
At first glance I'd say you should put GetStdHandle() into the main function rather than what you're doing now.
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%...
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";
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.
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);
}
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?
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
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.
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.