Easily change text color

This is a discussion on Easily change text color within the C++ Programming forums, part of the General Programming Boards category; If you are running a Win32 console app, I've made a header file which defines most text color schemes, for ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    267

    Easily change text color

    If you are running a Win32 console app, I've made a header file which defines most text color schemes, for example... the API command to change the console text color to red is
    Code:
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    So I simply #defined it like so
    Code:
    #define text_red SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    I went off on a tangent and ended up defining pretty much every color combination I could think of, and it took a long while to do, so for convienence, I put it here (it's actually 140 combinations )

    So now you can do this
    Code:
    #include "def_colors.h";
    #include <iostream>
    
    int main()
    {
    text_white_red;
    std::cout << "This text is white on red!";
    text_lightgrey_cyan;
    std::cout << "This text is light grey on cyan!";
    text_grey;
    std::cout << "This text is plain old grey on black...";
    
    return 0;
    }
    text_foreground_background

    Maybe I'm just wasting time.... oh well, I know it helped me since I prefer using API commands for console manipulation
    Attached Files Attached Files
    Last edited by d00b; 07-25-2002 at 11:10 AM.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    15
    You're a life saver! I was looking for something just like this.

    Thanx

  3. #3
    x4000 Ruski's Avatar
    Join Date
    Jun 2002
    Location
    Outer Space!
    Posts
    542
    what's with _HA NDLE? .. thx helped a lot though I used to use a function like textcolor(int) oh and is there something less platform-specific for changing colours in console? I know pascal does :P
    what does signature stand for?

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by Ruski
    what's with _HA NDLE?
    Thatis just what happens when the board displays a long string without a space. It inserts its own.

    Quote Originally Posted by d00b
    Maybe I'm just wasting time....
    Well I'm sure it will help a lot of noobs

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why #define? why not inline functions? why the repetitive code? why not wrap it up into a console_colour object or similar.Small example but could be much better refactored.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Yes. Please bundle your code up behind at least twenty-five layers of abstraction because I'm incapable of doing this in a manner that suits me.

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Its not about 25 layers of abstraction, its about writing correct code that wont break. #defines are brittle and can easily cause problems. Why should you have to write GetStdHandle(STANDARD_OUTPUT_HANDLE) over and over. It need only be done once.heres an article that should give some ideas on a better way of doing this.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    *this
    Join Date
    Mar 2005
    Posts
    498
    Heres an example of easier code...
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
    
    HANDLE hOUT = GetStdHandle(STD_OUTPUT_HANDLE);
    
    void setColor (bool forIntense, bool backIntense, int color) 
    {
       SetConsoleTextAttribute (hOUT, 
                               (forIntense ? FOREGROUND_INTENSITY : 0) |
                               (backIntense ? BACKGROUND_INTENSITY : 0) | 
                               (color));
    }
    
    int main () {
       setColor (1,0,FOREGROUND_RED);
       cout << "TEST!" << endl;
       setColor (1,0,FOREGROUND_BLUE);
       cout << "TEST!" << endl;
       setColor (1,1,BACKGROUND_RED);
       cout << "TEST!" << endl;
    
       cin.get();
       return 0;
    }

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Where is FOREGROUND_RED declared? In windows.h?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Woop?

  11. #11
    *this
    Join Date
    Mar 2005
    Posts
    498
    Quote Originally Posted by dwks
    Where is FOREGROUND_RED declared? In windows.h?
    Yes they are in windows.h

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. How do i change backround color and text color?
    By Nathan the noob in forum C++ Programming
    Replies: 1
    Last Post: 02-17-2009, 03:39 AM
  3. Change RichTextBox Color
    By C_ntua in forum C# Programming
    Replies: 8
    Last Post: 12-18-2008, 02:44 PM
  4. VC++ setting text color of a button
    By 7stud in forum Windows Programming
    Replies: 5
    Last Post: 10-14-2004, 11:04 AM
  5. Edit Text color for specific lines
    By JaWiB in forum Windows Programming
    Replies: 7
    Last Post: 07-08-2004, 03:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21