Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,629 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,053 people online right now. Registration is fast and FREE... Join Now!





Color Console C++

Add color text to console. Just check example its explained there.

Submitted By: okyup
Actions:
Rating:
Views: 10,018

Language: C++

Last Modified: November 15, 2006
Instructions: Compile and execute.

Snippet


  1. #include <stdlib.h>
  2. #include <windows.h>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. enum Color { DBLUE=1,GREEN,GREY,DRED,DPURP,BROWN,LGREY,DGREY,BLUE,LIMEG,TEAL,
  8.     RED,PURPLE,YELLOW,WHITE,B_B };
  9. /* These are the first 16 colors anyways. You test the other hundreds yourself.
  10.    After 15 they are all combos of different color text/backgrounds. */
  11.  
  12. bool quit;
  13.  
  14. void col(unsigned short color)
  15. {
  16.     HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
  17.     SetConsoleTextAttribute(hcon,color);
  18. }
  19.  
  20. istream &operator>> ( istream &in, Color &c )
  21. {
  22.     int tint;
  23.     cin >> tint;
  24.     if (tint==-1) quit=true;
  25.     c=(Color)tint;
  26. }
  27.  
  28. int main()
  29. {
  30.     do {
  31.         col(7); // Defaults color for each round.
  32.         cout << "Enter a color code, or -1 to quit... ";
  33.         Color y;
  34.         cin >> y; // Notice that >> is defined above for Color types.
  35.         col(y); // Sets output color to y.
  36.         if (!quit) cout << "Color: " << (int)y << endl;
  37.     } while (!quit);
  38.     return 0;
  39. }

Copy & Paste


Comments


kalaisubi 2008-09-06 12:52:13

syntax errors and compilation errors

chuck981996 2008-09-18 05:32:07

Very Good


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.




Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month