Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Colour in C++ !?!


ghost's Avatar
0 0

Okay, well I've been doing C++ for a while now (only the basics) and I'm wondering how to get colour in a console program. I know you can use: markupSystem("color 0c"); For example but that will change the whole screen colour to Black and Red. I'm wondering how to just change the colour of like one word at a time, and have another word in a different colour. I don't know if this can be done, infact I doubt it myself but if it can please let me know how. :)


ghost's Avatar
0 0

I don't think it's possible in C++, but it is in VB2005 if you create a console application.


SySTeM's Avatar
-=[TheOutlaw]=-
20 0

system("color [colour]"); cout<<"This is written in [colour]\n"; system("color [different colour]"); cout<<"And this is written in [different colour]\n";

Surely that'd work…


ghost's Avatar
0 0

Hehe, I was gonna post that. It won't work. It will change the colour of the whole window, then write some text, then change the colour of the entire window to the new colour.


ghost's Avatar
0 0

Yeah :(


ghost's Avatar
0 0

Man how does everyone obtain this info like.. cout<<([hibbity shabop])>>tuoc lol

I don't get C++ at all :|


ghost's Avatar
0 0

heres the long way: add windows.h to your includes, add a variable "HANDLE handle", make it valid to your console: handle=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);

then the function: SetConsoleTextAttribute(handle,(WORD)(backgroundcolor) | foregroundcolor));

both of which are ints, theyre the same as the int argument for color apart from instead of a its 10 etc. etc.

i know thats long but hope it helps lol


Uber0n's Avatar
Member
0 0

Nick wrote: Man how does everyone obtain this info like.. cout<<([hibbity shabop])>>tuoc lol

E-books, real books, google, forums, my school books… C++ is everywhere :evil:


ghost's Avatar
0 0

Uber0n wrote: [quote]Nick wrote: Man how does everyone obtain this info like.. cout<<([hibbity shabop])>>tuoc lol

E-books, real books, google, forums, my school books… C++ is everywhere :evil:[/quote]

:matey: Aarrggh!! xD


ghost's Avatar
0 0

mr noob wrote: heres the long way: add windows.h to your includes, add a variable "HANDLE handle", make it valid to your console: handle=CreateFile("CONOUT$",GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, 0L, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0L);

then the function: SetConsoleTextAttribute(handle,(WORD)(backgroundcolor) | foregroundcolor));

both of which are ints, theyre the same as the int argument for color apart from instead of a its 10 etc. etc.

i know thats long but hope it helps lol

Umm thanks I think but I don't understand it lol.