-
Colours?
Hi. I am still learning, so dont shout if this sounds silly. On a basic C++ program, is there any way to change screen colours/text colours. I have a few simple programs that would look better with (anything than black) different colour screens for different user entries. Lets say , when a clrscr(); is called maybe the background/screen could change for the next screen. Some of the available source (games)on the site seems to utilise different colours...what say you all?!:rolleyes:
-
rerepost,
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;
}
-
try adding this function:
void color(int color)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),color);
}
then call it with a number, i.e color(5); and it will change to a different color.
-
:) thanx you guys, this site is fantastic for this kinda stuff.:) :)
-
Here is a nice utility
Here is a simple utility I made to use in my AP Computer Science AB class. It works well with visual c++. Granted you could probably add more functionality to it, but all we really need in class is that added color to give it a final touch.
The location function appears to work but it seems a little erratic with odd buffer sizes.
It works with or without using the namespace version of iostream
We have a lot of problems with mismatched header files like that in class. As a matter of fact, does anyone know how to resolve namespace versus non-namespace header file conflicts with ease?
Code:
#ifndef _CONSOLE_LIBRARY_INTERFACE
#define _CONSOLE_LIBRARY_INTERFACE
#include <windows.h>
WORD cl_fc_param = 0;
WORD cl_bc_param = 0;
void text_color(bool red, bool green, bool blue, bool bright)
{
#ifdef _INC_IOSTREAM
cout.flush();
#endif
cl_fc_param = 0;
if ( red ) cl_fc_param |= FOREGROUND_RED;
if ( green ) cl_fc_param |= FOREGROUND_GREEN;
if ( blue ) cl_fc_param |= FOREGROUND_BLUE;
if ( bright ) cl_fc_param |= FOREGROUND_INTENSITY;
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), cl_fc_param|cl_bc_param );
}
void text_background(bool red, bool green, bool blue, bool bright)
{
#ifdef _INC_IOSTREAM
cout.flush();
#endif
cl_bc_param = 0;
if ( red ) cl_bc_param |= BACKGROUND_RED;
if ( green ) cl_bc_param |= BACKGROUND_GREEN;
if ( blue ) cl_bc_param |= BACKGROUND_BLUE;
if ( bright ) cl_bc_param |= BACKGROUND_INTENSITY;
SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), cl_fc_param|cl_bc_param );
}
void text_loc(int x, int y)
{
#ifdef _INC_IOSTREAM
cout.flush();
#endif
COORD cursor_coordinates;
cursor_coordinates.X = x;
cursor_coordinates.Y = y;
SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), cursor_coordinates );
}
void console_title(const char* title)
{
SetConsoleTitle(title);
}
#endif
-
Ok but after you use the colour, is it possible to tell the computer to switch back to normal colour?
Eg: To have a green title and normal coloured writing?
Paul
-
this is how to change colours:
void color(int color_num)
{
if( color_num == 1) // red
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
}
if( color_num == 2) // light grey
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, INTENSITY);
}
if( color_num == 3) // Blue
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE);
}
if( color_num == 4) // Cyan
{
HANDLE hStdout;
hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
}
then just pick a color for each text:
color(4);
cout<<"\n\n Cool colour\n"<<endl;
color(2);
cout<<" Now white colour"<<endl;
-
Switch colors
Sure, for example, if you use my small utility library above. Here is what you would do.
Code:
#include <iostream.h> // namespace ver works too
#include <util_lib_above.h>
void main()
{
text_color(1, 0, 0, 1); // red green blue bright
cout << "Hello Red World" << endl;
text_color(0, 1, 0, 1); // red green blue bright
cout << "Hello Green World" << endl;
}
You can achieve some cool effects in the console. Take this out for a test try.
Code:
#include <iostream.h> // namespace ver works too
#include <util_lib_above.h>
#include <stdlib.h>
void main()
{
while ( 1 )
{
if ( rand() % 2 )
{
text_background(0, 0, 0, 0);
text_color(0, 0, 0, 0);
}
else
{
text_background(1, 1, 1, 1);
text_color(1, 1, 1, 1);
}
text_loc( rand() % 80, rand() % 20 );
cout << " ";
}
}