Change text color using visual c++

Reply

Join Date: Aug 2004
Posts: 10
Reputation: hail2dthief is an unknown quantity at this point 
Solved Threads: 0
hail2dthief hail2dthief is offline Offline
Newbie Poster

Change text color using visual c++

 
0
  #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"))?
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: Change text color using visual c++

 
0
  #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 Quick reply to this message  
Join Date: Aug 2004
Posts: 10
Reputation: hail2dthief is an unknown quantity at this point 
Solved Threads: 0
hail2dthief hail2dthief is offline Offline
Newbie Poster

Re: Change text color using visual c++

 
0
  #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 Quick reply to this message  
Join Date: May 2006
Posts: 1
Reputation: Brian The Mad is an unknown quantity at this point 
Solved Threads: 0
Brian The Mad Brian The Mad is offline Offline
Newbie Poster

Re: Change text color using visual c++

 
0
  #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"))?
  1. #define BLACK 0
  2. #define BLUE 1
  3. #define GREEN 2
  4. #define CYAN 3
  5. #define RED 4
  6. #define MAGENTA 5
  7. #define BROWN 6
  8. #define LIGHTGREY 7
  9. #define DARKGREY 8
  10. #define LIGHTBLUE 9
  11. #define LIGHTGREEN 10
  12. #define LIGHTCYAN 11
  13. #define LIGHTRED 12
  14. #define LIGHTMAGENTA 13
  15. #define YELLOW 14
  16. #define WHITE 15
  17. #define BLINK 128
  18. HANDLE screen;
  19. int textcolor = LIGHTGREEN;
  20. int backgroundcolor = BLACK;
  21. screen = GetStdHandle(STD_OUTPUT_HANDLE);
  22. void TextColor(int fontcolor,int backgroundcolor,HANDLE screen)
  23. {
  24. int color_attribute;
  25. color_attribute = backgroundcolor;
  26. color_attribute = _rotl(color_attribute,4) | fontcolor;
  27. SetConsoleTextAttribute(screen,color_attribute);
  28. }
  29.  
  30. TextColor(textcolor,backgroundcolor ,screen);
  31. FillConsoleOutputAttribute(screen, _rotl(backgroundcolor,4) , 80 * 50,coord , &cWritten);


This works in Microsoft c++
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1
Reputation: MagikMan74 is an unknown quantity at this point 
Solved Threads: 0
MagikMan74 MagikMan74 is offline Offline
Newbie Poster

Re: Change text color using visual c++

 
0
  #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:
  1.  
  2. #include <windows.h>
  3. #include <iostream.h>
  4.  
  5. int main()
  6. {
  7. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //replace the 0 with a number for the color you want
  8.  
  9. cout << "Your text here" << endl;
  10.  
  11. return 0;
  12. }
Last edited by MagikMan74; Sep 26th, 2007 at 2:10 pm. Reason: edit: forgot the [code][/code]
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,382
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 217
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Change text color using visual c++

 
0
  #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 y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 11
Reputation: |\|asrin is an unknown quantity at this point 
Solved Threads: 0
|\|asrin |\|asrin is offline Offline
Newbie Poster

Re: Change background color using visual c++

 
0
  #7
Jan 22nd, 2009
HELLO
Can sombody tell me how can i chang the color of concol background????
Originally Posted by MagikMan74 View Post
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:
  1.  
  2. #include <windows.h>
  3. #include <iostream.h>
  4.  
  5. int main()
  6. {
  7. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0); //replace the 0 with a number for the color you want
  8.  
  9. cout << "Your text here" << endl;
  10.  
  11. return 0;
  12. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 36
Reputation: mostermand is an unknown quantity at this point 
Solved Threads: 1
mostermand mostermand is offline Offline
Light Poster

Re: Change text color using visual c++

 
0
  #8
Jan 22nd, 2009
Try this.

  1. 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 |
All i've got is a slice of pi
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Change background color using visual c++

 
0
  #9
Jan 22nd, 2009
Originally Posted by |\|asrin View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 11
Reputation: |\|asrin is an unknown quantity at this point 
Solved Threads: 0
|\|asrin |\|asrin is offline Offline
Newbie Poster

Re: Change text color using visual c++

 
0
  #10
Jan 23rd, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC