Thread: colour

  1. #1
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321

    colour

    Hi
    How would i change the colour of the text that is outputted... e.g

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout << "C++ rules!!!" << endl;
        cin.get();
        return 0;
    }
    Say if i wanted to change the
    Code:
     C++ Rules!!!"
    to red or something.. how would i do that?

    Thanks in advance

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's no way in standard C++.

    In Windows, there's a few API functions you can use for that. If forgot what they are.

    In *nix consoles, you do it by writing the special escape sequences to the console that are interpreted as commands to change colour or similar properties. Look for ANSI escape sequences.

    Curses defines its own functions for controlling colour.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    There's an FAQ topic on this.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Here's the link to the color FAQ.

  5. #5
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    when i do whats in the faq... i get an error:
    Code:
      [Linker error] undefined reference to `textcolor'
    Any ideas?

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    You need to find the function and delcare it in the source code.

    Code:
    void DrawColorString(string szText, int X, int Y, WORD color)
    {
       HANDLE OutputH;
      COORD position = {X, Y};
      OutputH = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(OutputH, color);
      SetConsoleCursorPosition(OutputH, position);
      cout << szText;
    }
    This works with:

    Code:
    DrawColorString("",0,5,FOREGROUND_RED);
    Double Helix STL

  7. #7
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    im using a console project, not win32

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    im using a console project, not win32
    Just include windows.h

    As Cat said, there is no standard way to invoke color in conole programs
    Double Helix STL

  9. #9
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Or you can use...

    Code:
    system ( "color 0C" );
    ...with the corresponding digit values:

    Code:
    0 = Black  	8 = Gray
    1 = Blue 	9 = Light Blue
    2 = Green 	A = Light Green
    3 = Aqua 	B = Light Aqua
    4 = Red 	C = Light Red
    5 = Purple 	D = Light Purple
    6 = Yellow  	E = Light Yellow
    7 = White 	F = Bright White

  10. #10
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    thank you... it works!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  2. Curses-- colour pairs
    By sufthingol in forum C++ Programming
    Replies: 0
    Last Post: 03-21-2005, 08:26 PM
  3. Replies: 5
    Last Post: 03-01-2003, 04:52 PM
  4. DirectDraw colour matching
    By Hunter2 in forum Game Programming
    Replies: 2
    Last Post: 12-10-2002, 02:17 PM
  5. Colour theory... (weird title)
    By Magos in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2001, 04:16 PM