Hello. I found this format of code on the internet. It makes only a certain selection of text a color instead of changing the whole console like with system ("color "). I am using the format but want to know what it all means, mostly just the HANDLE. I'll underline things I would like an explanation of.
hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
// Use the three primary colors for mixing any other color.
// Use FOREGROUND_INTENSITY for brighter colors.
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Bright red text\n";
(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "Bright blue text\n";
(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
cout << "Back to normal white text\n";
// Wait for user pressing key before exiting
// Gives them a chance to see the output
cout << "\n\nPress any key to exit program.....";
A handle is an object used to represent something more complex, usually such that the complex thing's internal workings are hidden from you.
For example, in C, you open a FILE* as:
FILE* fp = fopen( "foo.txt", "r" );
In this case, the variable [tt]fp is a handle -- it is a reference to a thing that represents a file. The internal workings of that thing are a black box -- you don't have any idea how it works. Indeed, it may be completely different depending for a different OS, or even different compilers. It doesn't matter to you, though, because you can use that handle to control the file object.
fread() fscanf() fgets()
fwrite() fprintf() fputs()
And so on. You don't have to know anything about the way the file is actually manipulated -- you can simply use it.
As an opaque object, handles may be a number of things, but the most common are pointers (like the FILE*) and integers (like a HANDLE).
Likewise, in the Windows API, there are a number of system objects that you can get handles to -- a window, a process, a file, an image, a mutex, a piece of memory. It doesn't really matter though, once you have a handle you can manipulate it.
In the code you have, the HANDLE is a system integer that represents the Windows Console output. To write text to the console window, or otherwise manipulate the console window, you use the handle to tell Windows exactly what you are trying to modify.