Change text color in C

This is a discussion on Change text color in C within the C Programming forums, part of the General Programming Boards category; hi... Can anyone tell me how to change text color in C? I'm working on a school project and i ...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    Change text color in C

    hi...

    Can anyone tell me how to change text color in C?

    I'm working on a school project and i would like to change the text color depending on a users level.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Compiler/OS?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey Dave!

    I'm using Visual Studio 2005 - Windows Vista x64

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    humm...
    i tryed this example, and it didn't work :-(

    Code:
    #include <stdio.h> 
    #include <conio.h> 
    
    int main ( void )
    {
      textcolor ( MAGENTA );
      cprintf ( "This is a test\n" );
    
      return 0;
    }
    ERROR: "error C2065: 'MAGENTA' : undeclared identifier"

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    *sigh*

    You said you were running on Windows (64-bit version!). Use the Win32 (Win64 anyway? )approach listed there, not the DOS one.

  7. #7
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    ups!
    lol...

    thx MacGyver, it's working nicely :-D

  8. #8
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    i dont understand what the codes does :-S

    Code:
      HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE ); //->whats this for? cant it be changed?
      WORD wOldColorAttrs; //->whats this for? cant it be changed?
      CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //->whats this for? cant it be changed?
      
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
      wOldColorAttrs = csbiInfo.wAttributes; 
      
      /*
       * Set the new color information
       */
      SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
      
      printf ( "This is a test\n" );
    
      /*
       * Restore the original colors
       */
      SetConsoleTextAttribute ( h, wOldColorAttrs);
      return 0;
    }
    This way i only have 5 possible colors, why?

    How can i now change the background color?

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    You have the RGB colors for both background and foreground. Using those three values and combining them, you can come up with other colors. Think about the combinations just foreground for a second:

    • Red
    • Green
    • Blue
    • Red + Green
    • Red + Blue
    • Blue + Green
    • Red + Green + Blue
    • Black (ie. No RGB)


    Going through the code now....

    Code:
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE ); //->whats this for? cant it be changed?
    HANDLE is a Windows API datatype that is just a another way of saying void *. It's a pointer to a "handle". Handles, and the many other similar types in Windows, are generally pointers to files and the like.

    In this case, if you remember, when you write a C program, you have 3 "files" already opened for you:

    • stdout
    • stdin
    • stderr


    You may not realize you're using them. When you use printf(), you're writing to stdout. When you use scanf(), you're reading from stdin.

    The code gets the HANDLE associated with stdout.

    Code:
    WORD wOldColorAttrs; //->whats this for? cant it be changed?
    This is a WORD variable that is going to be used to store the old data color info. You generally want to do this because if you change the color of the console text in your application, when your program is terminated, the color will not revert back to what it was before. That is your job to set it back to normal.

    Code:
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo; //->whats this for? cant it be changed?
    This is another one of those large structs that Microsoft has. The name of the datatype tells you that it contains information on the console.

    Code:
      /*
       * First save the current color information
       */
      GetConsoleScreenBufferInfo(h, &csbiInfo);
      wOldColorAttrs = csbiInfo.wAttributes;
    The comment says this is one way of saving the current console text color scheme.

    Code:
      /*
       * Set the new color information
       */
      SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY );
    Obviously this changes the color of the text. The interesting thing you should realize is that it performs this on HANDLE h. Remember, h is just a HANDLE to the file representing stdout. This is how Windows will know which console window you want to change the color.

    Code:
      printf ( "This is a test\n" );
    Obvious.

    Code:
      /*
       * Restore the original colors
       */
      SetConsoleTextAttribute ( h, wOldColorAttrs);
    Simple restore the colors back to what they were before you changed them. That way when your program quits, the colors are back to normal.

  10. #10
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    wow!!
    tnx alot MacGyver, you rock!
    couldn't ask for a better explanation ;-)

    going to start some color testing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to use FTP?
    By maxorator in forum C++ Programming
    Replies: 8
    Last Post: 11-04-2005, 02:17 PM
  2. change color text?
    By k4z1nh0 in forum C Programming
    Replies: 4
    Last Post: 03-15-2005, 10:41 AM
  3. help with calculating change from a dollar
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2002, 03:58 PM
  4. change text
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 08-23-2002, 08:28 PM
  5. Replies: 2
    Last Post: 09-04-2001, 02:12 PM

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