User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,985 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,789 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 34355 | Replies: 5
Reply
Join Date: Aug 2004
Posts: 10
Reputation: hail2dthief is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hail2dthief hail2dthief is offline Offline
Newbie Poster

Change text color using visual c++

  #1  
Aug 27th, 2004
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"))?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2004
Posts: 251
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Rep Power: 6
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Change text color using visual c++

  #2  
Aug 27th, 2004
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
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote  
Join Date: Aug 2004
Posts: 10
Reputation: hail2dthief is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
hail2dthief hail2dthief is offline Offline
Newbie Poster

Re: Change text color using visual c++

  #3  
Aug 29th, 2004
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++?
Reply With Quote  
Join Date: May 2006
Posts: 1
Reputation: Brian The Mad is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Brian The Mad Brian The Mad is offline Offline
Newbie Poster

Re: Change text color using visual c++

  #4  
May 4th, 2006
Originally Posted by hail2dthief
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++
Reply With Quote  
Join Date: May 2007
Posts: 1
Reputation: MagikMan74 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
MagikMan74 MagikMan74 is offline Offline
Newbie Poster

Re: Change text color using visual c++

  #5  
Sep 26th, 2007
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;
}
Last edited by MagikMan74 : Sep 26th, 2007 at 2:10 pm. Reason: edit: forgot the [code][/code]
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 487
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 73
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Change text color using visual c++

  #6  
Sep 26th, 2007
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
  1. enum Colors { blue=1, green, cyan, red, purple, yellow, grey, dgrey, hblue, hgreen, hred, hpurple, hyellow, hwhite };
  2.  
  3. void coutc(int color, char* output)
  4. {
  5. HANDLE handle= GetStdHandle(STD_OUTPUT_HANDLE);
  6. SetConsoleTextAttribute( handle, color);
  7. cout<< output;
  8. SetConsoleTextAttribute( handle, color);
  9. }

Then to output in color you would just do
  1. coutc(red, "This is in red!");
  2. coutc(purple, "This is purple!");
Last edited by ShawnCplus : Sep 26th, 2007 at 5:48 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h-->++ r z+*
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC