Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C++ Source Code


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

Join 244,183 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,348 people online right now. Registration is fast and FREE... Join Now!





Setting console colour

Makes the console out put nice and pretty! WINDOWS SPECIFIC!

Submitted By: gabehabe
Actions:
Rating:
Views: 456

Language: C++

Last Modified: July 13, 2008
Instructions: Use TestColours(int i) for ***testing purposes only*** You don't need to use it in any programs, it's just for you to pick which colours you like the most =)

Snippet


  1. /*
  2. * The simplest way to have colourful text in the console
  3. * Author: Danny Battison
  4. * Contact: gabehabe@hotmail.com
  5. */
  6.  
  7. #include <iostream> /* Standard input/output stream */
  8. #include <windows.h> /* Standard Windows API header */
  9.  
  10. void SetColour (int index) /* Set the colour, just pass the index */
  11. {
  12.     /* Pass the console handle and the index (param) to the Win API function */
  13.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), index);
  14. }
  15.  
  16. void ResetColour () /* Reset the colour to the default (white) */
  17. {
  18.     /* Pass 7 as the colour for the original white text colour */
  19.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
  20. }
  21.  
  22. /* This following function is just for testing purposes */
  23. void TestColours (int i) /* Test the colours to see which key you want to use */
  24. {
  25.     if (i == 255) /* Change this number if you want to loop higher */
  26.         return; /* Exit the recursive function if the number has been reached */
  27.     else
  28.         SetColour (i); /* Set the colour and output it */
  29.         std::cout << "colour key: " << i << std::endl;
  30.         TestColours (++i); /* Increment i and pass it recursively */
  31. }
  32.  
  33. int main()
  34. {
  35.     std::cout << "This is the original colour for text..." << std::endl;
  36.     SetColour (5);
  37.     std::cout << "This text should be colourful!" << std::endl;
  38.     ResetColour ();
  39.     std::cout << "This is the original colour for text again..." << std::endl;
  40.     std::cin.get (); /* Pause before testing the colours */
  41.     TestColours (0); /* Test the colours with a recursive function */
  42.  
  43.     std::cin.get(); /* Hold the window open */
  44.     return EXIT_SUCCESS; /* The program was executed successfully */
  45. }
  46.  
  47.  

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!

Be Social

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

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month