🔒 Locked
[redacted anon]
Advertisement
Original Post
unders dos/windows there is (there was) a non-standard library called conio.h that let you handle coloured texts (as well as other things). I don't know if it is providen with all compilers, nor if it is supported anymore.
Other than that (if you can) you could use win32 functions.
EDIT: if you want to skip win32 functions and you're using mingw, then you may try this conio library.
Other than that (if you can) you could use win32 functions.
EDIT: if you want to skip win32 functions and you're using mingw, then you may try this conio library.
You can use SetConsoleTextAttribute() to change the colour of console text in Win32, but you'll have to include the window.h header.
Take a look at this quick example I made:
As you can see, changing the 2nd parameter changes the colour of the text. Use an RGB colour mixing chart to see which colours used together will make what.
Good luck!
Take a look at this quick example I made:
#include <iostream>#include <windows.h>using namespace std;int main(){ SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED); cout<<"Moo\n"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE); cout<<"Moo\n"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN); cout<<"Moo\n"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE); cout<<"Moo\n"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_RED); cout<<"Moo\n"; SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE); cout<<"Moo\n"; cin.get(); return 0;}As you can see, changing the 2nd parameter changes the colour of the text. Use an RGB colour mixing chart to see which colours used together will make what.
Good luck!
Ah ha! that was perfect. Much appreciated Undead my friend, thank you all for your help but that was fairly simple, I've always tended to avoid things like that in C++ because many I've heard believe such Windows functions are slow, over complicated, etc. But this suits my purpose nicely :) Thankyou!
More colors!!
#include <iostream>#include <windows.h>using namespace std;enum ConsoleTextColor{ GREY, WHITE, RED, GREEN, BLUE, PURPLE, CYAN, BRIGHT_RED, BRIGHT_GREEN, BRIGHT_BLUE, LAVANDER, AQUA, YELLOW, GOLD};void SetConsoleTextColor(ConsoleTextColor cl){ WORD newColor; HANDLE ScreenBuffer = GetStdHandle(STD_OUTPUT_HANDLE); switch(cl) { case GREY: newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN; break; case WHITE: newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; case RED: newColor = FOREGROUND_RED; break; case GREEN: newColor = FOREGROUND_GREEN; break; case BLUE: newColor = FOREGROUND_BLUE; break; case PURPLE: newColor = FOREGROUND_RED | FOREGROUND_BLUE; break; case CYAN: newColor = FOREGROUND_GREEN | FOREGROUND_BLUE; break; case BRIGHT_RED: newColor = FOREGROUND_RED | FOREGROUND_INTENSITY; break; case BRIGHT_GREEN: newColor = FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; case BRIGHT_BLUE: newColor = FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; case LAVANDER: newColor = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; case AQUA: newColor = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY; break; case YELLOW: newColor = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY; break; case GOLD: newColor = FOREGROUND_RED | FOREGROUND_GREEN; break; } SetConsoleTextAttribute(ScreenBuffer, newColor);}int main(){ ConsoleTextColor textColor; for(int i = 0; i<=GOLD; i++) { textColor = (ConsoleTextColor)i; SetConsoleTextColor(textColor); cout << "Moo." << endl; } return 0;}
I believe you might want SetConsoleCursorPosition();
This may not interest you at the current time since you've got a project to do, but this console tutorial by Ben Ryves is also very interesting.
This may not interest you at the current time since you've got a project to do, but this console tutorial by Ben Ryves is also very interesting.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement