Simple Explanation of HANDLE and Others

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
 #include <windows.h>
 using namespace std;
 int main()
 {

 HANDLE hConsole;
    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";



    SetConsoleTextAttribute

    (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);

            cout << "Bright blue text\n";



    SetConsoleTextAttribute

    (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.....";

            cin.get();



        return 0;

}

Any help would be greatly appreciated. Thank you!
By the way, the website I aquired this from is: http://www.dreamincode.net/forums/topic/19910-text-and-background-color-in-c/
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.

Hope this helps.
That helped very much. Thank you.
Topic archived. No new replies allowed.