text colour

This is a discussion on text colour within the C Programming forums, part of the General Programming Boards category; Hello again, I was recently going thru some source code to try and pinpoint how to change the text colour ...

  1. #1
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96

    text colour

    Hello again,
    I was recently going thru some source code to try and pinpoint how to change the text colour in dos, anyways so this is what i found...

    Code:
    #include <stdio.h>
    #include <windows.h> // <---*THIS IS THE REQUIRED HEADER FILE FOR IT TOO WORK
    
    int main()
    {
    
    
    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    
        WORD wOldColorAttrs;
    
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    
        GetConsoleScreenBufferInfo(h, &csbiInfo);  
        wOldColorAttrs = csbiInfo.wAttributes; 
    
    
        SetConsoleTextAttribute ( h, FOREGROUND_RED | FOREGROUND_INTENSITY ); //Set the new color information
    
    printf("TEXT"); //*THESE LINES PRINT THE TEXT
    
     -Curtosy Author of 'Adventures Of You'
    Iv'e got a fair idea of whas going on here, but if someone could explain to me the hows and why's of this code that would be awsome.

  2. #2
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26
    Code:
    #include <windows.h>
    
    main() {
    
      system("color fc");
    
      return 0;
    }
    This one will change the background to white and letters to red.

    Type on the prompt "color help" and it will show various color choices there. Then I add whatever color I wanna change to when I write on the code, "system(color /*whatever color*/"); then it changes when you run the program.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,751
    Unfortunately that wasn't the question, Utopus.

    A good place to start when you work with Windows code is usually MSDN. The relevant things here are

    As for what part of the structure you changed in your code, there is a WORD in CONSOLE_SCREEN_BUFFER_INFO structures that you can shift around to a specific color, but you are limited to about eight.

    Word to the wise: always maintain new and old attributes, because this makes it easy to switch colors back and forth, and you should at least return the console's settings back to the defaults when you are finished messing with color. Also, poorly commented code should be for the most part avoided. Don't comment poorly yourself, and seek out explained examples or documentation when you are learning or looking up something.

  4. #4
    Registered User Utopus's Avatar
    Join Date
    Jun 2007
    Location
    beautiful place
    Posts
    26
    >Unfortunately that wasn't the question, Utopus.

    Sorry, I'm a noobie.

  5. #5
    Master n00b Matty_Alan's Avatar
    Join Date
    Jun 2007
    Location
    Bloody Australia Mate!
    Posts
    96
    thanks guys i'll run through those docs now and i'll see what i can get out of em'

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Consult the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    And MacGyver did a damn good job of explaining the code from the FAQ, Change text color in C

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Adrianxw has also some nice tutorials
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

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, 03:17 PM
  2. Replies: 1
    Last Post: 07-13-2002, 06:45 PM
  3. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM
  4. To change text colour
    By Dirty Harry in forum C Programming
    Replies: 2
    Last Post: 06-16-2002, 04:04 AM
  5. Setting text colour
    By JamMan in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 11:43 AM

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