You have the RGB colors for both background and foreground. Using those three values and combining them, you can come up with other colors. Think about the combinations just foreground for a second:
- Red + Green
- Red + Blue
- Blue + Green
- Red + Green + Blue
- Black (ie. No RGB)
Going through the code now....
HANDLE is a Windows API datatype that is just a another way of saying void *. It's a pointer to a "handle". Handles, and the many other similar types in Windows, are generally pointers to files and the like.
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE ); //->whats this for? cant it be changed?
In this case, if you remember, when you write a C program, you have 3 "files" already opened for you:
You may not realize you're using them. When you use printf(), you're writing to stdout. When you use scanf(), you're reading from stdin.
The code gets the HANDLE associated with stdout.
This is a WORD variable that is going to be used to store the old data color info. You generally want to do this because if you change the color of the console text in your application, when your program is terminated, the color will not revert back to what it was before. That is your job to set it back to normal.
WORD wOldColorAttrs; //->whats this for? cant it be changed?
This is another one of those large structs that Microsoft has. The name of the datatype tells you that it contains information on the console.
CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //->whats this for? cant it be changed?
The comment says this is one way of saving the current console text color scheme.
* First save the current color information
wOldColorAttrs = csbiInfo.wAttributes;
Obviously this changes the color of the text. The interesting thing you should realize is that it performs this on HANDLE h. Remember, h is just a HANDLE to the file representing stdout. This is how Windows will know which console window you want to change the color.
* Set the new color information
SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
printf ( "This is a test\n" );
Simple restore the colors back to what they were before you changed them. That way when your program quits, the colors are back to normal.
* Restore the original colors
SetConsoleTextAttribute ( h, wOldColorAttrs);