Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 85,025 C++ Programmers. There are 1,268 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!


Easy cout color change

with this (header) file, its very easy to change the ostream(cout) color.

Submitted By: Over
Actions:
Rating:
Views: 77

Language: C++

Last Modified: December 31, 1969
Instructions: u can use it as a header file or copy and past it into your cpp.



Snippet


  1. #include <iostream>
  2. using namespace std;
  3. #include <windows.h>
  4.  
  5. #define RED             0x0004
  6. #define GREEN             0x0002
  7. #define BLUE             0x0001
  8. #define WHITE             RED|GREEN|BLUE
  9. #define YELLOW       RED|GREEN
  10. #define PINK             RED|BLUE
  11. #define TURQUOISE    BLUE|GREEN
  12. #define BG_RED             0x0040
  13. #define BG_GREEN     0x0020
  14. #define BG_BLUE             0x0010
  15. #define BG_WHITE     BG_RED|BG_GREEN|BG_BLUE
  16. #define BG_YELLOW    BG_RED|BG_GREEN
  17. #define BG_PIN       KBG_RED|BG_BLUE
  18. #define BG_TURQUOISE BG_BLUE|BG_GREEN
  19.  
  20. typedef unsigned short int usint;
  21. //---------------------------------------------------------------------------------       
  22. class ostreamHelper
  23. {
  24.         private:
  25.                         usint n_;
  26.                         bool inten_;
  27.                         ostream& (*f_)(ostream&,usint,bool);
  28.         public:
  29.                         ostreamHelper(ostream&(*f)(ostream&,usint, bool), usint n, bool inten):f_(f),n_(n),inten_(inten) { }
  30.                         friend ostream& operator<<(ostream& os,ostreamHelper helper)
  31.                         {
  32.                                 return helper.f_(os,helper.n_,helper.inten_); }
  33. };
  34. //---------------------------------------------------------------------------------       
  35. ostream& ColHelper(ostream& os, usint col, bool inten)
  36. {
  37.         HANDLE *hOut=new HANDLE;
  38.         *hOut=GetStdHandle(STD_OUTPUT_HANDLE);
  39.         if(inten==true)
  40.                 col+=0x0008;
  41.         SetConsoleTextAttribute(*hOut,col);
  42.         delete hOut;
  43.         return os;
  44. }
  45. //--------------------------------------------------------------------------------       
  46. ostreamHelper col(usint col, bool intensiv)
  47. {
  48.         return ostreamHelper(&ColHelper,col,intensiv);
  49. }
  50. //---------------------------------------------------------------------------------       
  51.  
  52.  
  53. //EXAMPLES HOW TO USE
  54. int main()
  55. {
  56.         cout << col(RED,true) << "hi, I'm an intensiv Red :D\n";
  57.         cout << "I'm still red, couz u dont change it!\n";
  58.         cout << col(YELLOW|BG_GREEN,true) << "hi, I'm yellow and my BG is greeen :D\n";
  59.         cout << col(YELLOW,false) << "now, my color is not intensiv :(\n";
  60.         cout << col(WHITE,false) << "now I have the standard color. :> great.\n";
  61.         return 0;
  62. }

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


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





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month