943,342 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 131720
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 27th, 2004
0

Change text color using visual c++

Expand Post »
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"))?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hail2dthief is offline Offline
10 posts
since Aug 2004
Aug 27th, 2004
0

Re: Change text color using visual c++

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
Reputation Points: 108
Solved Threads: 7
Posting Whiz in Training
FireNet is offline Offline
256 posts
since May 2004
Aug 29th, 2004
0

Re: Change text color using visual c++

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++?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hail2dthief is offline Offline
10 posts
since Aug 2004
May 4th, 2006
0

Re: Change text color using visual c++

Quote 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"))?
C++ Syntax (Toggle Plain Text)
  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++
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Brian The Mad is offline Offline
1 posts
since May 2006
Sep 26th, 2007
0

Re: Change text color using visual c++

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:
C++ Syntax (Toggle Plain Text)
  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]
Reputation Points: 10
Solved Threads: 0
Newbie Poster
MagikMan74 is offline Offline
1 posts
since May 2007
Sep 26th, 2007
0

Re: Change text color using visual c++

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
c++ Syntax (Toggle Plain Text)
  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
c++ Syntax (Toggle Plain Text)
  1. coutc(red, "This is in red!");
  2. coutc(purple, "This is purple!");
Last edited by ShawnCplus; Sep 26th, 2007 at 5:48 pm.
Sponsor
Reputation Points: 520
Solved Threads: 268
Code Monkey
ShawnCplus is offline Offline
1,564 posts
since Apr 2005
Jan 22nd, 2009
0

Re: Change background color using visual c++

HELLO
Can sombody tell me how can i chang the color of concol background????
Click to Expand / Collapse  Quote originally posted by MagikMan74 ...
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:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
|\|asrin is offline Offline
11 posts
since Jan 2009
Jan 22nd, 2009
0

Re: Change text color using visual c++

Try this.

CPP Syntax (Toggle Plain Text)
  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 |
Reputation Points: 12
Solved Threads: 1
Light Poster
mostermand is offline Offline
36 posts
since Sep 2008
Jan 22nd, 2009
0

Re: Change background color using visual c++

Click to Expand / Collapse  Quote originally posted by |\|asrin ...
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?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jan 23rd, 2009
0

Re: Change text color using visual c++

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
|\|asrin is offline Offline
11 posts
since Jan 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading multiple .txt files from input folder
Next Thread in C++ Forum Timeline: Need help with output errors.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC