Text color

This is a discussion on Text color within the C++ Programming forums, part of the General Programming Boards category; How do you change the color of text being printed into the console from my program?...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    19

    Text color

    How do you change the color of text being printed into the console from my program?
    Jacob Sheehy

  2. #2
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    i would also like to know, and how to do it on msvc++ if anyone knows?
    Paro

  3. #3
    Registered User blackwyvern's Avatar
    Join Date
    Jan 2002
    Posts
    60
    Syntax
    #include <conio.h>

    void textcolor(int _color);

    Description
    Sets just the foreground of the text attribute.

  4. #4
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    could you make a sample program for me?
    Paro

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    145
    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main
    {
    
    textcolor(LIGHTBLUE);  // Change font color to Light Blue
    textbackground(RED);  // Change background color to Red
    clrscr();  // Needed before changes will take effect
    
    cout<< "Hello.\n";
    
    textcolor(LIGHTGRAY);  // Default DOS Prompt text color
    textbackground(BLACK);  // Default DOS Prompt background color
    clrscr();
    return(0);
    }
    There. Easy as that. Make sure to put a clrscr() after you change the color, otherwise there will be no effect. Look through the conio.h library to see all of the colors. There are some light versions of colors as well, so make sure to check the library. Hope that helps.

    Kyoto Oshiro
    Horizon Games
    http://www.angelfire.com/realm2/horizon/files.html

  6. #6
    Registered User blackwyvern's Avatar
    Join Date
    Jan 2002
    Posts
    60
    I am getting some wierd compiler errors from running that program:

    I cant seem to get the colors to work


    C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x19):redcolor.cp p: undefined reference to `textcolor'
    C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x26):redcolor.cp p: undefined reference to `textbackground'
    C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x2e):redcolor.cp p: undefined reference to `clrscr'
    C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x4d):redcolor.cp p: undefined reference to `textcolor'
    C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x5a):redcolor.cp p: undefined reference to `textbackground'
    C:\WINDOWS\TEMP\ccVUM9fb.o(.text+0x62):redcolor.cp p: undefined reference to `clrscr'


    whats wrong? Im using Dev-C++

  7. #7
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160

    Question

    ok, i copy and pasted that...

    added "()" after main so it would work...

    added the code for clrscr() to work
    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <windows.h>
    void clrscr();
    int main ()
    {
    
    textcolor(LIGHTBLUE);  // Change font color to Light Blue
    textbackground(RED);  // Change background color to Red
    clrscr();  // Needed before changes will take effect
    
    cout<< "Hello.\n";
    
    textcolor(LIGHTGRAY);  // Default DOS Prompt text color
    textbackground(BLACK);  // Default DOS Prompt background color
    clrscr();
    return(0);
    }
    void clrscr()
    {
       COORD coordScreen = { 0, 0 };
       DWORD cCharsWritten;
       CONSOLE_SCREEN_BUFFER_INFO csbi;
       DWORD dwConSize;
       HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
       FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
       GetConsoleScreenBufferInfo(hConsole, &csbi);
       FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
       SetConsoleCursorPosition(hConsole, coordScreen);
    }
    (code i used above)

    i use MSVS 6.0 btw...

    and i compile and this is what i get

    --------------------Configuration: PrimeNumber - Win32 Debug--------------------
    Compiling...
    Prime.cpp
    C:\Programming\PrimeNumber\Prime.cpp(8) : error C2065: 'textcolor' : undeclared identifier
    C:\Programming\PrimeNumber\Prime.cpp(8) : error C2065: 'LIGHTBLUE' : undeclared identifier
    C:\Programming\PrimeNumber\Prime.cpp(9) : error C2065: 'textbackground' : undeclared identifier
    C:\Programming\PrimeNumber\Prime.cpp(9) : error C2065: 'RED' : undeclared identifier
    C:\Programming\PrimeNumber\Prime.cpp(14) : error C2065: 'LIGHTGRAY' : undeclared identifier
    C:\Programming\PrimeNumber\Prime.cpp(15) : error C2065: 'BLACK' : undeclared identifier
    Error executing cl.exe.

    PrimeNumber.exe - 6 error(s), 0 warning(s)
    Any clues?
    Paro

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    145

    Question Well...

    I am using the free v 5.5 compiler off the Borland website. I do all my colors this way, and it seems to work fine. Then again, I don't do all that in the void clrscr() stuff Paro. And sorry about the missing () after int main...not enough sleep

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int main()
    {
    
    textcolor(WHITE);
    textbackground(BLACK);
    clrscr();
    
    cout<< "\aHello.\n\n";
    
    return(0);
    }
    If that doesn't work, then I don't know. I have never used either of the compilers you have mentioned, so that might be a reason. Wierd.

    Kyoto Oshiro

  9. #9
    Registered User Paro's Avatar
    Join Date
    Feb 2002
    Posts
    160
    it must be the compiler...cuz it just wont work for me

    and i thought msvc++ was better than the common freebie!

  10. #10
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    This has been asked many times. Conio.h is a non-standard header so the ones that have made the compiler has, in your case, probably decided not to take most of its functions.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    27

    Setting color

    Above you say things like "LightGrey". Can I use either HEX or RGB to define the colors?

    thanks,

    Greg
    Chance favors the prepared mind.

    Vis. C++ 6.0

  12. #12
    Registered User
    Join Date
    Sep 2001
    Posts
    19
    So how is it done with VC++?
    Jacob Sheehy

  13. #13
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Colors in console for msvc:
    Code:
    the colors can be mixed 
    
    FOREGROUND_BLUE Text color contains blue. 
    FOREGROUND_GREEN Text color contains green. 
    FOREGROUND_RED Text color contains red. 
    FOREGROUND_INTENSITY Text color is intensified. 
    BACKGROUND_BLUE Background color contains blue. 
    BACKGROUND_GREEN Background color contains green. 
    BACKGROUND_RED Background color contains red. 
    BACKGROUND_INTENSITY Background color is intensified. 
    
    
    #include <windows.h>
    #include <stdio.h>
    
    int main( void )
    {
    
        HANDLE hStdout;
    
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
        
        printf("WWWWOOOOO MY TEXT IS RED WWOOOOO!!");
    
        return 0;
    }
    Guys this has been posted like a thousand times,a search would have found it.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  14. #14
    Unregistered
    Guest

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    19
    Hey, sorry for not searching, I'm usually very good about that. Sorry. Your code works great, thank you! When you say that "the colors can be mixed", how do you do that? thanks
    Jacob Sheehy

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Text Color Problem
    By justlivelife15 in forum Windows Programming
    Replies: 3
    Last Post: 06-25-2007, 05:47 AM
  2. Change text color in C
    By IndioDoido in forum C Programming
    Replies: 9
    Last Post: 04-15-2007, 05:54 AM
  3. Critique my lighting model.
    By psychopath in forum Game Programming
    Replies: 4
    Last Post: 08-12-2006, 06:23 PM
  4. Color text from windows.h?
    By Saintdog in forum C++ Programming
    Replies: 10
    Last Post: 12-03-2004, 08:20 AM
  5. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM

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