Color Variety

This is a discussion on Color Variety within the C++ Programming forums, part of the General Programming Boards category; does anyone know how to make the output text a color in c++ for conio.h and windows.h all for BORLAND ...

  1. #1
    Unregistered
    Guest

    Color Variety

    does anyone know how to make the output text a color in c++
    for conio.h and windows.h all for BORLAND C++ 5.x ?

    p.s.

    how does everybody use those code tage on the board that allow tabbing ?

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

    Thumbs up

    I know how to make the text a different color using the conio.h library. I am using the free compiler (Borland v 5.5.1) off their website, so it may be different for you. Anyhow, it is really quite simple, look at the code below.

    ----------------------------

    #include <iostream.h>
    #include <conio.h>

    int main()
    {
    textcolor(CYAN);
    textbackground(BLACK);
    clrscr();

    cout<< "This text color should be cyan.\n";
    getch();

    return(0);
    }

    ----------------------------

    Make sure to put the colors of the font/background in all capitals. Check the conio.h library for the full list of colors, it is pretty easy to spot.

    Hope that helps.


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

  3. #3
    D3T
    D3T is offline
    Registered User
    Join Date
    Feb 2002
    Posts
    12
    Can you do that repeatedly?
    like

    Code:
    backgroundcolor(BLACK);
    clrscr();
    textcolor(RED);
    cout << "red";
    textcolor(BLUE);
    cout << "blue";
    -or whatever?
    [D3T]
    Borland Turbo C++ 3.0 For Dos

    *
    do { war(); } while (nations == hostile);

    ... The best way to prevent wars is to have them.

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

    Smile

    You can change the color as much as you want, there is only one thing you have to remember. After each change of color (background or font) you need to use a clrscr() for the changes to take effect. And remember, to change the background it is textbackground(COLOR), not backgroundcolor(COLOR) as you had put.

    Hope that helps.


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

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I'll post this so no-one dosent have to rererepost
    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()
    {
    
        HANDLE hStdout;
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
        
        printf("WWWWOOOOO MY TEXT IS RED WWOOOOO!!");
        return 0;
    }
    You can change color without having to clear the screen if you use this, providing of course that you have windows.h
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Using windows.h, how do you make white?

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Heres a snippet from my console library.
    Code:
    namespace Stoned_Console
    {     // names are prepended with T for Text and B for Background. TRED for instance is red text.
    	const unsigned short BLACK				=	0;
    	const unsigned short TRED				=	FOREGROUND_RED;
    	const unsigned short BRED				=	BACKGROUND_RED;
    	const unsigned short TGREEN				=	FOREGROUND_GREEN;
    	const unsigned short BGREEN				=	BACKGROUND_GREEN;
    	const unsigned short TBLUE				=	FOREGROUND_BLUE;
    	const unsigned short BBLUE				=	BACKGROUND_BLUE;
    	const unsigned short TCYAN				=	FOREGROUND_BLUE | FOREGROUND_GREEN;
    	const unsigned short BCYAN				=	BACKGROUND_BLUE | BACKGROUND_GREEN;
    	const unsigned short TMAGENTA			=	FOREGROUND_RED | FOREGROUND_BLUE;
    	const unsigned short BMAGENTA			=	BACKGROUND_RED | BACKGROUND_BLUE;
    	const unsigned short TYELLOW			=	FOREGROUND_RED | FOREGROUND_GREEN;
    	const unsigned short BYELLOW			=	BACKGROUND_RED | BACKGROUND_GREEN;
    	const unsigned short TDKGREY			=	FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN;
    	const unsigned short BDKGREY			=	BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN;
    	const unsigned short TLTGREY			=	FOREGROUND_INTENSITY;
    	const unsigned short BLTGREY			=	BACKGROUND_INTENSITY;
    	const unsigned short TLTRED				=	FOREGROUND_RED | FOREGROUND_INTENSITY;
    	const unsigned short BLTRED				=	BACKGROUND_RED | BACKGROUND_INTENSITY;
    	const unsigned short TLTGREEN			=	FOREGROUND_GREEN | FOREGROUND_INTENSITY;
    	const unsigned short BLTGREEN			=	BACKGROUND_GREEN | BACKGROUND_INTENSITY;
    	const unsigned short TLTBLUE			=	FOREGROUND_BLUE | FOREGROUND_INTENSITY;
    	const unsigned short BLTBLUE			=	BACKGROUND_BLUE | BACKGROUND_INTENSITY;
    	const unsigned short TLTCYAN			=	FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
    	const unsigned short BLTCYAN			=	BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
    	const unsigned short TLTMAGENTA			=	FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
    	const unsigned short BLTMAGENTA			=	BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
    	const unsigned short TLTYELLOW			=	FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
    	const unsigned short BLTYELLOW			=	BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_INTENSITY;
    	const unsigned short TWHITE				=	FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
    	const unsigned short BWHITE				=	BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY;
    
    				
    
    void colour(const unsigned short); // sets text and bkground colours
    }
    Code:
    void Stoned_Console::colour(const unsigned short colinfo)
    
    	{
    		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), colinfo);
    	}
    just bitwise or | the background colour and the text colour u want in the call.
    i.e.
    colour(BWHITE|TYELLOW);
    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
    Registered User GreenCherry's Avatar
    Join Date
    Mar 2002
    Posts
    65
    Nevermind; you add green, blue and red.
    I have a rabbit in my pants! Please be happy for me.

Popular pages Recent additions subscribe to a feed

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