0

can anyone explain this code to me ? This code can set font color in c++ (window only) and i copy from this web site (http://www.dreamincode.net/code/snippet1491.htm) it work , but i don't understand some line (<-)


1.void setcolor(unsigned int color)
2.{
3. if (color >15 || color <=0)
4. {
5. cout <<"Error" <<endl;
6.
7. }
8. else
9. {
10. HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);<-
11. SetConsoleTextAttribute(hcon,color); <-
12. }
13.}

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by sfuo
0

Windows uses handles for letting the user/program control elements of the Windows environment (not 100% sure how to explain this).

So line 10 is getting the output handle and saving its memory address into the pointer hcon. This is then passed into SetConsoleTextAttribute() on line 11 and has its color attribute modified and since hcon is being modified and it is a pointer then the true console controller is then modified and the settings are changed.

Correct me if I'm wrong but I'm pretty sure this is generally how it works.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.