Is there a way to do color in a console app? Im using VC++ 6.0. Its just to jazz parts up. anyone?
Printable View
Is there a way to do color in a console app? Im using VC++ 6.0. Its just to jazz parts up. anyone?
the colors can be mixed
FOREGROUND_BLUE Text color contains blue.
FOREGROUND_GREEN Text color contains green.
FOREGROUND_RED Text color contains red.
FOREGROUND_INTENSITY Text color is intensified.
BACKGROUND_BLUE Background color contains blue.
BACKGROUND_GREEN Background color contains green.
BACKGROUND_RED Background color contains red.
BACKGROUND_INTENSITY Background color is intensified.
Code:
#include <windows.h>
int main()
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
printf("WWWWOOOOO MY TEXT IS RED WWOOOOO!!");
return 0;
}
THANKS!!
But, Can I have multiple text on the screen??
PHP Code:
#include <windows.h>
#include <iostream.h>
void color(int color_num)
{
if( color_num == 1)
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
}
if( color_num == 2)
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE);
}
}
int main()
{
color(2);
cout << " RED ";
color(1);
cout << " BLUE";
return 0;
}
This outbuts both in the same color....so????
any ideas?
ACAC
Visual C++ only changes the color for one line. You'll have to put them on seperate lines, then use SetConsoleTextAttribute again
In other words use
color(2);
cout << " RED "<<endl; //or you could use \n
color(1);
cout << " BLUE"<<endl; //same here \n
I've tried this multiple ways, but I can't get a, lets say, a blue background w/ green text. Is there a way to accomplish this?
Thanks!
Mike
i've tried it and Background colors will not change... so i don't know either, sorry.
Are there any different colors that can be used?
Thanks
Not really. The problem is that ostream is buffered. Try:Quote:
Visual C++ only changes the color for one line. You'll have to put them on seperate lines, then use SetConsoleTextAttribute again
Code:color(2);
cout << " RED " << flush;
color(1);
cout << " BLUE" << flush;
See here: Windows Programming FAQQuote:
I've tried this multiple ways, but I can't get a, lets say, a blue background w/ green text. Is there a way to accomplish this?
with this code....
color(2);
cout << " RED " << flush;
color(1);
cout << " BLUE" << flush;
***sorry about the previous one...hit a reply by accident***
with this code....
color(2);
cout << " RED " << flush;
color(1);
cout << " BLUE" << flush;
what header do you use ?
Quote:
Originally Posted by Unregistered
Hi!, How I can return to Black and White???
I don't see how this is a game programming related thread?
ehhh Thread necrophilia!
You can have a blue text on a green background. You have to mix colors to achieve this. You would OR the foreground and background flags like so:
You would then be able to toggle the color using the function SetConsoleTextAttribute. You would need to call this function each time you want to change the color.Code:WORD color_attribute=FOREGROUND_BLUE|BACKGROUND_GREEN
With a windows console the screen buffer is an array of CHAR_INFO structures. There are several windows console functions that work with CHAR_INFO arrays to modify the color and text and then display them to the screen. You can goto the msdn library for a complete list of Windows console functions.