944,111 Members | Top Members by Rank

Ad:
  • C++ Code Snippet
  • Views: 69252
  • C++ RSS
2

Add a little Color to your Console Text

by on Nov 7th, 2004
Things don't have to be black and white all the time. Use a Windows API call to add some color to your text output.
C++ Code Snippet (Toggle Plain Text)
  1. // color your text in Windows console mode
  2. // colors are 0=black 1=blue 2=green and so on to 15=white
  3. // colorattribute = foreground + background * 16
  4. // to get red text on yellow use 4 + 14*16 = 228
  5. // light red on yellow would be 12 + 14*16 = 236
  6. // a Dev-C++ tested console application by vegaseat 07nov2004
  7.  
  8. #include <iostream>
  9. #include <windows.h> // WinApi header
  10.  
  11. using namespace std; // std::cout, std::cin
  12.  
  13. int main()
  14. {
  15. HANDLE hConsole;
  16. int k;
  17.  
  18. hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  19.  
  20. // you can loop k higher to see more color choices
  21. for(k = 1; k < 255; k++)
  22. {
  23. // pick the colorattribute k you want
  24. SetConsoleTextAttribute(hConsole, k);
  25. cout << k << " I want to be nice today!" << endl;
  26. }
  27.  
  28. cin.get(); // wait
  29. return 0;
  30. }
  31.  
Comments on this Code Snippet
Oct 15th, 2006
0

Re: Add a little Color to your Console Text

Nice...
How do you change the background color?
Pretty please?
And is there a way to make the program appear fullscreen when started up?
Junior Poster
venomlash is offline Offline
143 posts
since Oct 2006
Jul 21st, 2007
0

Re: Add a little Color to your Console Text

I have tried to use this in my program.

However when I used #include<windows.h> I get errors when I compile.

I am using the MSVisual 6.0 ???
Newbie Poster
Akilah712 is offline Offline
21 posts
since Jun 2007
May 31st, 2008
0

Re: Add a little Color to your Console Text

on windows only:
system("color <put your colors here>");

colors the whole window and all text to any of the standard 16 colors

//0 = Black 8 = Gray
//1 = Blue 9 = Light Blue
//2 = Green a = Light Green
//3 = Aqua b = Light Aqua
//4 = Red c = Light Red
//5 = Purple d = Light Purple
//6 = Yellow e = Light Yellow
//7 = White f = Bright White

you put two characters, first one is background color, second is text color:
system("color c0"); //colors background to light red, with black text
Newbie Poster
mistercow.pnoy is offline Offline
7 posts
since Feb 2008
Jun 27th, 2009
0

Re: Add a little Color to your Console Text

ya, bakround color use . - system("color f0"); and #include <stdlib.h> it will make the bacround wight with black text for more color codes type 'color help' in cmd prompt. the first nuber ids the backround and the seconed is the text. also to start the consol in full screen send the keys alt and enter like this

keybd_event(VK_MENU, 0x38, 0, 0);
keybd_event(VK_RETURN, 0x1c, 0, 0);
keybd_event(VK_RETURN, 0X1c, KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
Newbie Poster
dombit is offline Offline
10 posts
since Jun 2009
Sep 18th, 2010
-1

Re: Add a little Color to your Console Text

Just what I was looking for. Cheers!
Newbie Poster
setherith is offline Offline
16 posts
since Aug 2010
Jan 11th, 2011
0

Re: Add a little Color to your Console Text

Nice one the "system" function...who'd have thought it Thanks!
Newbie Poster
jamesysco is offline Offline
1 posts
since Jan 2011
Feb 15th, 2011
0

Re: Add a little Color to your Console Text

To get all the system() commands (WINDOWS ONLY!), open up the command prompt (start>accessories>Command Prompt), and type "help" (without the quotes). For help on a specific command, type "help <command name>" (again, no quotes).
Newbie Poster
pwnedu46 is offline Offline
1 posts
since Feb 2011
Jan 14th, 2012
-1

Creating background and text color in c++ console application

c++ Syntax (Toggle Plain Text)
  1. #include<conio.h>
  2. #include<iostream>
  3. #include<dos.h>
  4. int main() {
  5. textbackground(BLUE);
  6. textcolor(YELLOW);
  7. std::cout<<"Yellow text in a blue console application";
  8. std::cin.get();
  9. return 0;
  10. }
Newbie Poster
shirish7151 is offline Offline
1 posts
since Jan 2012
Message:
Previous Thread in C++ Forum Timeline: Detecting 3D Images How?
Next Thread in C++ Forum Timeline: Where is OBJECT in the class?





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


Follow us on Twitter


© 2011 DaniWeb® LLC