Thread: Colours?

  1. #1
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158

    Question Colours?

    Hi. I am still learning, so dont shout if this sounds silly. On a basic C++ program, is there any way to change screen colours/text colours. I have a few simple programs that would look better with (anything than black) different colour screens for different user entries. Lets say , when a clrscr(); is called maybe the background/screen could change for the next screen. Some of the available source (games)on the site seems to utilise different colours...what say you all?!
    Such is life.

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    rerepost,

    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.

    Code:
    #include <windows.h>
    
    int main()
    {
    
        HANDLE hStdout;
        hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
        
        printf("WWWWOOOOO MY TEXT IS RED WWOOOOO!!");
        return 0;
    }
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    85
    try adding this function:


    void color(int color)
    {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE),color);
    }

    then call it with a number, i.e color(5); and it will change to a different color.

  4. #4
    Much older and wiser Fountain's Avatar
    Join Date
    Dec 2001
    Location
    Engeeeerland
    Posts
    1,158

    Smile

    thanx you guys, this site is fantastic for this kinda stuff.
    Such is life.

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    Here is a nice utility

    Here is a simple utility I made to use in my AP Computer Science AB class. It works well with visual c++. Granted you could probably add more functionality to it, but all we really need in class is that added color to give it a final touch.

    The location function appears to work but it seems a little erratic with odd buffer sizes.

    It works with or without using the namespace version of iostream
    We have a lot of problems with mismatched header files like that in class. As a matter of fact, does anyone know how to resolve namespace versus non-namespace header file conflicts with ease?

    Code:
    #ifndef _CONSOLE_LIBRARY_INTERFACE
    #define _CONSOLE_LIBRARY_INTERFACE
    
    #include <windows.h>
    WORD cl_fc_param = 0;
    WORD cl_bc_param = 0;
    
    void text_color(bool red, bool green, bool blue, bool bright)
    {
    #ifdef _INC_IOSTREAM
    	cout.flush();
    #endif
    
    	cl_fc_param = 0;
    	if ( red ) cl_fc_param |= FOREGROUND_RED;
    	if ( green ) cl_fc_param |= FOREGROUND_GREEN;
    	if ( blue ) cl_fc_param |= FOREGROUND_BLUE;
    	if ( bright ) cl_fc_param |= FOREGROUND_INTENSITY;
    
    	SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), cl_fc_param|cl_bc_param );
    }
    void text_background(bool red, bool green, bool blue, bool bright)
    {
    #ifdef _INC_IOSTREAM
    	cout.flush();
    #endif
    
    	cl_bc_param = 0;
    	if ( red ) cl_bc_param |= BACKGROUND_RED;
    	if ( green ) cl_bc_param |= BACKGROUND_GREEN;
    	if ( blue ) cl_bc_param |= BACKGROUND_BLUE;
    	if ( bright ) cl_bc_param |= BACKGROUND_INTENSITY;
    
    	SetConsoleTextAttribute( GetStdHandle(STD_OUTPUT_HANDLE), cl_fc_param|cl_bc_param );
    }
    void text_loc(int x, int y)
    {
    #ifdef _INC_IOSTREAM
    	cout.flush();
    #endif
    	COORD cursor_coordinates;
    	cursor_coordinates.X = x;
    	cursor_coordinates.Y = y;
    	SetConsoleCursorPosition( GetStdHandle(STD_OUTPUT_HANDLE), cursor_coordinates );
    }
    
    void console_title(const char* title)
    {
    	SetConsoleTitle(title);
    }
    
    #endif

  6. #6
    Unregistered
    Guest
    Ok but after you use the colour, is it possible to tell the computer to switch back to normal colour?

    Eg: To have a green title and normal coloured writing?

    Paul

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    5
    this is how to change colours:

    void color(int color_num)
    {
    if( color_num == 1) // red
    {
    HANDLE hStdout;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hStdout, FOREGROUND_RED);
    }

    if( color_num == 2) // light grey
    {

    HANDLE hStdout;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hStdout, INTENSITY);

    }
    if( color_num == 3) // Blue
    {

    HANDLE hStdout;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE);

    }
    if( color_num == 4) // Cyan
    {

    HANDLE hStdout;
    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hStdout, FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);

    }



    }

    then just pick a color for each text:

    color(4);
    cout<<"\n\n Cool colour\n"<<endl;
    color(2);
    cout<<" Now white colour"<<endl;

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    Switch colors

    Sure, for example, if you use my small utility library above. Here is what you would do.

    Code:
    #include <iostream.h> // namespace ver works too
    #include <util_lib_above.h>
    
    void main()
    {
        text_color(1, 0, 0, 1); // red green blue bright
        cout << "Hello Red World" << endl;
        text_color(0, 1, 0, 1); // red green blue bright
        cout << "Hello Green World" << endl;
    }
    You can achieve some cool effects in the console. Take this out for a test try.

    Code:
    #include <iostream.h> // namespace ver works too
    #include <util_lib_above.h>
    #include <stdlib.h>
    
    void main()
    {
        while ( 1 ) 
        {
            if ( rand() % 2 ) 
            {
    	text_background(0, 0, 0, 0);
    	text_color(0, 0, 0, 0);
            }
            else 
            {
    	text_background(1, 1, 1, 1);
    	text_color(1, 1, 1, 1);
            }
    
            text_loc( rand() % 80, rand() % 20 );
            cout << " ";
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. RGB Colours
    By rogster001 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 04-22-2008, 12:56 PM
  2. Printf in different colours?
    By JFonseka in forum C Programming
    Replies: 7
    Last Post: 12-27-2007, 02:45 AM
  3. Colours in DOS box
    By denizengt in forum Tech Board
    Replies: 1
    Last Post: 09-10-2003, 06:53 AM
  4. it's not colours
    By Skarr in forum Game Programming
    Replies: 0
    Last Post: 05-13-2003, 12:21 PM
  5. Changing Text/Background colours for window controls ...
    By Shag in forum Windows Programming
    Replies: 1
    Last Post: 11-16-2002, 11:57 AM