1,105,399 Community Members

Change text color using visual c++

Member Avatar
hail2dthief
Newbie Poster
10 posts since Aug 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm using visual c++ compiler and I want to change the text color in my c++ dos program. What choices do I have if i don't wanna use system function (example: system("color 0a"))?

Member Avatar
FireNet
Posting Whiz in Training
256 posts since May 2004
Reputation Points: 64 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

conio.h

It has a lot funtions for console input/output.Take a look at it.

TextColor(RED);
TextBackground(BLUE);

The color values range from 0-15 with 0 being black and 15 being white

warning,i am not too sure of the funtion names,been a while since i used this, though i used it almost everywhere, man time flies

Member Avatar
hail2dthief
Newbie Poster
10 posts since Aug 2004
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks, FireNet. But the trick doesn't work for microsoft visual c++ 6.0 compiler, is there any function that i can use to change text color using visual c++?

Member Avatar
Brian The Mad
Newbie Poster
1 post since May 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm using visual c++ compiler and I want to change the text color in my c++ dos program. What choices do I have if i don't wanna use system function (example: system("color 0a"))?

#define BLACK 0
#define BLUE 1
#define GREEN 2
#define CYAN 3
#define RED 4
#define MAGENTA 5
#define BROWN 6
#define LIGHTGREY 7
#define DARKGREY 8
#define LIGHTBLUE 9
#define LIGHTGREEN 10
#define LIGHTCYAN 11
#define LIGHTRED 12
#define LIGHTMAGENTA 13
#define YELLOW 14
#define WHITE 15
#define BLINK 128
HANDLE  screen;
int textcolor = LIGHTGREEN;
int backgroundcolor = BLACK;
screen = GetStdHandle(STD_OUTPUT_HANDLE);
void TextColor(int fontcolor,int backgroundcolor,HANDLE screen)
{
   int color_attribute;
   color_attribute = backgroundcolor;
   color_attribute = _rotl(color_attribute,4) | fontcolor;
   SetConsoleTextAttribute(screen,color_attribute);
}
 
TextColor(textcolor,backgroundcolor ,screen); 
FillConsoleOutputAttribute(screen, _rotl(backgroundcolor,4) , 80 * 50,coord , &cWritten);

This works in Microsoft c++

Member Avatar
MagikMan74
Newbie Poster
1 post since May 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Of course, if you're just wanting to change the color as you're typing it, then the above is a bit much. A simpler way would be to use this:

#include <windows.h>
#include <iostream.h>
 
int main()
{
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //replace the 0 with a number for the color you want
 
   cout << "Your text here" << endl;
 
   return 0;
}
Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
0
 

I used this one to save me some time
This is not Visual C++ dependant, I use this with DEv-C++ as well, you just need the windows header

enum Colors { blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite };

void coutc(int color, char* output)
{
   HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
   SetConsoleTextAttribute( handle, color);
   cout<< output;
   SetConsoleTextAttribute( handle, color);
}

Then to output in color you would just do

coutc(red, "This is in red!");
coutc(purple, "This is purple!");
Member Avatar
|\|asrin
Newbie Poster
14 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HELLO
Can sombody tell me how can i chang the color of concol background????

Of course, if you're just wanting to change the color as you're typing it, then the above is a bit much. A simpler way would be to use this:

#include <windows.h>
#include <iostream.h>
 
int main()
{
   SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //replace the 0 with a number for the color you want
 
   cout << "Your text here" << endl;
 
   return 0;
}
Member Avatar
mostermand
Light Poster
36 posts since Sep 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Try this.

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED BACKGROUND_INTENSITY);

You can change RED to GREEN or BLUE and BACKGROUND to FOREGROUND.

If you want to mix colors you just separate them with a |

Member Avatar
Comatose
Taboo Programmer
2,536 posts since Dec 2004
Reputation Points: 290 [?]
Q&As Helped to Solve: 227 [?]
Skill Endorsements: 34 [?]
Team Colleague
 
0
 

HELLO
Can sombody tell me how can i chang the color of concol background????

HELLO... can someone tell me how I can get people to quit posting on ancient old threads?

Member Avatar
|\|asrin
Newbie Poster
14 posts since Jan 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi

Thank you for your reply mostermand but that code didnt work in
microsoft vitual c++(I dont know why??). after searching in the net for hours I found a
useful site about win32 consul application, I thought that it would be very useful for beginners .
http://www.adrianxw.dk/SoftwareSite/index.html

Bye

Member Avatar
phr34k1sh
Newbie Poster
1 post since Mar 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Alright... this is all i do... inside of main... right before i access cout... i simply just put on a line before this...

system("color 0a");

This is processing a batch command... and batch is quite simple... if you would like a definition of how this works, simply go to "cmd.exe" and type "help color" this will explain how to use this... you can change the background color of the console with this method... here is an example...

#include "header.h"

using namespace std;

int main ()
{
	system("title Hello World.");
	system("color 0a");
	cout<<"Hello world.";
	system("pause >nul");
	return 0;
}

header.h contains
#include <iostream>

so if you're using an older c++ compiler... replace
#include "header.h"
with
#include <iostream>

otherwise you know what to do... peace... hope i could help.

Member Avatar
justice igwe
Newbie Poster
6 posts since Jan 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

Thanks, FireNet. But the trick doesn't work for microsoft visual c++ 6.0 compiler, is there any function that i can use to change text color using visual c++?

am justice igwe, well am not so sure how ur programming skills are but anyway just figured this trick out. have u tried the #include<graphics.h> and #include<ctype.h> header files.. i think they will do the work for u ..

Member Avatar
msehgal
Newbie Poster
1 post since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

can i get any links for having all the graphics functions refernce in micosoft visual c++ express edition
anyone who can refer me any e-book on visual c++ ,..........
thnks alott in advance yaar

Member Avatar
gadsmuck
Newbie Poster
2 posts since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thanx

Member Avatar
maxicube
Light Poster
32 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
#include<iostream>
#include<Windows.h>

using namespace std;

int main()
{
	for(int i=0; i<255; i++)
	{
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), i);
	cout << "supppppppppppp " << i << endl;
	}
	system("pause");

}
Member Avatar
JadG
Newbie Poster
1 post since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

How do you make individual pieces of text be differently coloured

Member Avatar
Im_Numma_One
Newbie Poster
1 post since Mar 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Here is a cool way to look at how to select colors. I learned this from an assembly language class.

(foreColor + ( backgroundColor * 16))

So basically you can go at it like this:

int fColor;
int bColor;

cout << "Color choices and corresponding numbers:" << endl;
cout << "BLACK 0" << endl;
cout << "BLUE 1" << endl;
cout << "GREEN 2" << endl;
cout << "CYAN 3" << endl;
cout << "RED 4" << endl;
cout << "MAGENTA 5" << endl;
cout << "BROWN 6" << endl;
cout << "LIGHTGREY 7" << endl;
cout << "DARKGREY 8" << endl;
cout << "LIGHTBLUE 9" << endl;
cout << "LIGHTGREEN 10" << endl;
cout << "LIGHTCYAN 11" << endl;
cout << "LIGHTRED 12" << endl;
cout << "LIGHTMAGENTA 13" << endl;
cout << "YELLOW 14" << endl;
cout << "WHITE 15" << endl;

cout << "Please make your selections." << endl;

cout << "Text color: ";
cin >> fColor;

cout << "Background color: ";
cin >> bColor;

SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), (fColor + (bColor * 16)) );
cout << "Cool Tricks Cool Tricks"<< endl;
Member Avatar
suneel khatri
Newbie Poster
1 post since May 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello Friends this is simple code to change the text color or backgroung color in dev-C++ software....

#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
using namespace std;

int main()
{

    HANDLE a = GetStdHandle (STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(a, FOREGROUND_RED | FOREGROUND_INTENSITY);
            cout << "Bright red text\n";

    //SetConsoleTextAttribute(a, 4 | FOREGROUND_INTENSITY);
            //cout << "\n\nBright red text\n";

    SetConsoleTextAttribute(a, FOREGROUND_RED | BACKGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY );
            cout << "\n\nBack to normal white text\n";

    //SetConsoleTextAttribute(a, 4 | 2 | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY );
            //cout << "\n\nBack to normal white text\n";

            cout << "\n\nPress any key to exit program.....";
            cin.get();

        return 0;
}
Member Avatar
thefasninja1
Light Poster
34 posts since Dec 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I was also checking for the same color but is there any one who can tell me the number and its color, or share list of all number and color.
I am using system funtion to use color.

Member Avatar
Ancient Dragon
Achieved Level 70
27,643 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,037 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
0
 

Read the previous post -- some of the colors are already in that post and they are all defined in windows.h. Console programs have very limited colors, the same as old MS-DOS 6.X from 30 years ago.

You
Post:
Start New Discussion
Tags Related to this Article