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.}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.