| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Console Text Color
Hi all,
After hours of searching the internet for a solution on how to change the color of the console text color i found this #include <stdio.h> #include <windows.h> // WinApi header int main() { HANDLE hConsole; int k; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // you can loop k higher to see more color choices for(k = 1; k < 255; k++) { SetConsoleTextAttribute(hConsole, k); printf("%3d %s\n", k, "I want to be nice today!"); } getchar(); // wait return 0; After modifying it because it would not work on Borland 5.5, i came up with this : #include <iostream> #include <windows> // WinApi header using namespace std; int main() { HANDLE hConsole; int k = 10; hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, k); cout << "Color in console!\n", k; //Green Text Color cin.get(); return 0; } Now what i want to know is how can i have the text change to any color i want when i want? At the moment i can only use one color at a time. If i change the color to another color just before i want to use it, the compiler says it's an error. What would i have to do to make this work? I'm using windows XP, and Borland 5.5 commandline compiler. I hope i have been specific enough. Many Thanks in advance. Paul820 |
|
#2
|
||||
|
||||
|
I'm not sure how things work in windows but for linux you can use ANSI colour codes. Second hit on google seems to explain nicely:
http://www.tldp.org/linuxfocus/Engl...rticle335.shtml There is a sample with C code. Let me know if this also works for Windows. |
|
#3
|
|||
|
|||
|
Tried it
Quote:
I went to the site you said and tried the bit of C code that was there. I had to download pelles c compiler to see if it would work on it. It never even worked on a c compiler. I modified it a bit to try and get it to work on Borland, it worked, erm sort of, it just printed out (\033[0;40;32m) <--- That bit, nothing else. Maybe i will have to do a bit more searching. I didn't think getting the colors to change would be so complicated. Oh by the way, i used this code from that site : #include <stdio.h> int main(void){ const char *const green = "\033[0;40;32m"; const char *const normal = "\033[0m"; printf("%sHello World%s\n", green, normal); return 0; } Thanks for answering my question. I'll keep plodding on. Many thanks Paul820 |
|
#4
|
||||
|
||||
|
Ok I had time to check it out myself.. It does not work, sorry.. I compiled your code on my linux box and it worked. Then I cross-compiled it (i have no c compilers on my windows box and cross-compiling windows programs from linux just rocks!) but it did not work on windows. Which is could have known in advance since those escape codes are some terminal-specific thing.. I'll look into some more if I can.
|
|
#5
|
||||
|
||||
|
After some digging I see that the trick you are using is the way to do it (under C# there might be a console object but we are not using .not).. _plodding on_ as well..
|
|
#6
|
|||
|
|||
|
I've done it!!!
After some digging I see that the trick you are using is the way to do it (under C# there might be a console object but we are not using .not).. _plodding on_ as well..[/QUOTE]
Well have loads of digging around, reading bits of code i finally got it to work so i can use any color when i want. Heres the code: #include <iostream> #include <windows> using namespace std; int main() { HANDLE hConsole; hConsole = GetStdHandle (STD_OUTPUT_HANDLE); SetConsoleTextAttribute (hConsole, FOREGROUND_RED | FOREGROUND_INTENSITY); cout << "Red Text\n"; SetConsoleTextAttribute (hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY); cout << "Blue Text\n"; cin.get(); return 0; } There are loads of people on google trying to find out how to do it but no answers. If anyone wants to use it go ahead. Thanks for pointing me in the right direction Icon, appreciate that, couldn't have done it without you. Now i can get on with doing some more programming (with color). By the way, it's compiles with free Borland C++ 5.5, don't know about any others. (UPDATED) Just found out it isn't quite right yet, got ahead of myself there. It only lets you use three colors RED, BLUE and GREEN. Looks like it's back to the drawing board again paul820 Last edited by Paul820 : February 8th, 2006 at 07:26 PM. Reason: It isn't quite right afterall |
|
#7
|
||||
|
||||
|
Ok I'm at work now, so I don't have time to do anything myself (nor do I have a C(++) compiler here *gasp*). But I found this website which explains how to make character attributes:
http://msdn.microsoft.com/library/d...een_buffers.asp The idea is to 'mix' the three primary colours and an intensity to obtain any colour you want (think of the colour palettes in photoshop or paint). If I have time tonight (doubt it, sorry) then I'll try to elaborate on it. Hope this helps you a little. Note: So in fact you were already there, you just didn't know it, keep the drawing board ![]() Last edited by Icon : February 9th, 2006 at 01:40 AM. |
|
#8
|
||||
|
||||
|
A big example (not just about colours though) can be found here:
http://msdn.microsoft.com/library/d...functions.as p In your first example you are ranging k from 0 to 255. The character attribute parameter is of type Word which is unsigned 16 bit (2 bytes) which ranges from 0..65535 so ... ![]() |
|
#9
|
|||
|
|||
|
Mixing colors
Quote:
Thanks Icon, i never thought of mixing the colors, i assumed i could just use the colors by name, like FOREGROUND_YELLOW. I saw another example similar that did this. Never entered my mind about mixing the colors though. Going to have a good read on microsofts website now to see what else i can find out. Thanks for the links. So i did do it after all, sort of, with your help anyway, thanks. Paul820 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Console Text Color |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|