How do I change the colour of my text in a DOS program (or is this possible...?)
Thanks
-Chris
Printable View
How do I change the colour of my text in a DOS program (or is this possible...?)
Thanks
-Chris
It depends on your compiler. My DOS compiler, Borland 3.1, has a special cprintf() function that prints in color if you set it beforehand with textcolor() and textbackground(). I don't know how you'd do it with cout, though.
Does anybody know how to do this with cout?
What compiler do you have? If the answer is VC++6, than you can't do it without building a Win32 MFC or ATL project. DOS is mostly unsupported now a days, infact you have to work in a Win32 console in VC++6.
I have Dev C++ and Rhide (I can use both)
well, i've unfortunately had the experience that Dev-C++ doesn't allow interrupts... (unless someone found out... but i'm pretty sure that question is dead...) so, i never did end up switching from DJGPP to Dev-C++... (because i really liked the editor...) if you can find ways to access conventional memory using Dev-C++, i believe the address for the text is 0xb800:0000... and with DJGPP use int 10h... (a good resource for interrupts is Ralph Browns Interrupt List... by the way...)
hth...
There are lots of defined constants in windows.h. For text colour and background colour the ones we are interested in are:-
FOREGROUND_RED
FOREGROUND_GREEN
FOREGROUND_BLUE
FOREGROUND_INTENSITY
BACKGROUND_RED
BACKGROUND_GREEN
BACKGROUND_BLUE
BACKGROUND_INTENSITY
first you need to get the output handle for the console.Use GetStdHandle(STD_OUTPUT_HANDLE)
for actually setting the colour we use SetConsoleTextAttribute(hConsole,DWORD colour)
so to put it all together for instance to make red text on a bright blue background we can say:-
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),FOREGROUND_RED|BACKGROUND_BLUE|BACKGROUND_IN TENSITY);
Actually for some reason it works with cout << if you use clrscr() first (at least with borland command line, don't know with dev-cpp).
its all great but are there really only 3 colors?
Not quite.... you can get 16 colours....
All you do is 'or' the different constants together.... i.e.
FOREGROUND_RED|FOREGROUND_GREEN should give you yellow etc.
if you want a brighter colour or in FOREGROUND_INTENSITY etc.
play around with them... by using different combinations of the constants you can get 16 different colours.