Thread: Simple method of coloured text?

  1. #1
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35

    Question Simple method of coloured text?

    I'm writing a console application in DevC++ 4, I want to do coloured text.
    I've searched the forum, but it's pretty much just left me more confused.
    I don't want to use conio because it's non-standard, and I'd rather not use windows.h because I don't understand it.
    Is there another way?
    If not, could someone explain the windows.h method?
    - Well that's my 4c Australian.

  2. #2
    harryp
    Guest
    Well, I hate to break it to you, but those are the two easiest ways of doing the colors. windows method is fairly easy:

    SetConsoleTextAttribute(HANDLE handle, WORD color);

    Parameter 1: HANDLE handle -
    This is a HANDLE to the window's output buffer (that does sound confusing, doesn't it?). To make this simple, make a global variable like so:
    Code:
    HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    Now you've got the handle to the output so you can manipulate the text.

    Parameter 2:
    This parameter is the foreground/background color of the text. The colors are as follows:
    Code:
    FOREGROUND_RED // A dark red for the text
    FOREGROUND_GREEN // A dark green
    FOREGROUND_BLUE // A dark blue
    FOREGROUND_INTENSITY // Makes the text colors brighter (so dark red would become red)
    BACKGROUND_RED // A dark red for the text background
    BACKGROUND_GREEN
    BACKGROUND_BLUE
    BACKGROUND_INTENSITY // Brightens the text background color
    Those are very basic colors. You can combine them to make more. Like, if you wanted yellow text on a blue background, here would be the code:
    Code:
    SetConsoleTextAttribute(stdOut, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_INTENSITY);
    Finally, here's my last example:
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    // The HANDLE
    HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    
    int main()
    {
         // Make red text
         SetConsoleTextAttribute(stdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
         cout << "Red text!\n";
         return 0;
    }
    I hope that helps a little. Just ask if you need clarification.

    Brendan

  3. #3
    Registered User SPiRiToFCaT's Avatar
    Join Date
    May 2002
    Posts
    35

    Talking

    Thanks!
    I pretty much understand that now, you've been a big help.
    I'll play with it a bit then come back if I need more help.
    Also, does windows.h work on other platforms?
    - Well that's my 4c Australian.

  4. #4
    Seven years? civix's Avatar
    Join Date
    Jul 2002
    Posts
    605
    I'm not sure...I don't think it will work for Linux, but it might work for a pure MS-DOS Operating System...

    Hehe, all this OS talk makes me look smart!
    .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  2. Replies: 5
    Last Post: 02-01-2003, 10:58 AM
  3. text simple question
    By Unregistered in forum Game Programming
    Replies: 2
    Last Post: 04-26-2002, 09:45 AM
  4. simple text dialog ok cancel
    By Brian in forum Windows Programming
    Replies: 1
    Last Post: 02-12-2002, 02:20 AM