1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to make colored output

Discussion in 'C' started by jan1024188, Dec 31, 2006.

  1. jan1024188

    jan1024188 New Member

    Dec 31, 2006
    Likes Received:
    Trophy Points:
    ok here I want to know how to make colorated output in C and C++...

    For example: I want to print this string "zelena". The color must be green. Now how to get green output using printf() in C and cout << "zelena";
  2. NewsBot

    NewsBot New Member

    Dec 2, 2008
    Likes Received:
    Trophy Points:
  3. HowardL

    HowardL New Member

    Aug 5, 2007
    Likes Received:
    Trophy Points:
    East Coast USA
    You consider this to be beginner stuff ??? hmmm...

    Well it gets involved... Character Mode Application Programming.

    In Linux you would use the curses library. (ncurses)
    I haven't done that for a while.

    I have been trying to learn win32 API for this lately.
    Here are a few things to try (msdn reference snippet at bottom of code):
    #include <stdio.h>
    #include <windows.h>
    void PAUSE(void) {while(getc(stdin) != '\n');}
    int main(void)
      HANDLE hOut;   /* handle */
      COORD  Pos;    /* struct to hold coordinates of a single cell */
      int i;
      FreeConsole();   /* process leaves the initial calling console */
      AllocConsole();  /*        and starts a new console for itself */
                     /* You could skip these two but it might screw up the output already
                     on your calling console by printing over it unless you clear screen first.*/
      hOut = GetStdHandle(STD_OUTPUT_HANDLE); /* assign to the main stdout buffer */
      printf("Hello from here!");     /* usual old printf output */
      Pos.Y =  3;                     /* assign coordinates */
      Pos.X = 25;
      SetConsoleCursorPosition(hOut, Pos);  /* moves cursor to the coordinates */
      SetConsoleTextAttribute(hOut, 0x01);   /* sets background and foreground colors */
      printf("zelena 0x01");
      for(i= 2; i < 0x0b; i++) {
        SetConsoleCursorPosition(hOut, Pos);
        SetConsoleTextAttribute(hOut, i);
        printf("zelena 0x%02x", i);
      Pos.Y +=2;
      SetConsoleCursorPosition(hOut, Pos);
      SetConsoleTextAttribute(hOut, 0x20);
      printf("zelena 0x20");
      SetConsoleCursorPosition(hOut, Pos);
      SetConsoleTextAttribute(hOut, 0xa0);
      printf("and finally: zelena 0xa0");
      SetConsoleTextAttribute(hOut, 0x07);
      printf("     Now we're here \n");
      printf("\n...and now we're\n back to \n printing as normal \n");
      PAUSE();    /* pause to read screen before it disappears as process ends
                     since this console was allocated                          */
      return 0;
    It all starts here:  (have fun!)
    Specifically speaking about attributes:
    Character Attributes
    Character attributes can be divided into two classes: color and DBCS.
    The following attributes are defined in the Wincon.h header file.
    Attribute           my '98 0x vals      Meaning
    FOREGROUND_BLUE              1       Text color contains blue.
    FOREGROUND_GREEN             2       Text color contains green.
    FOREGROUND_RED               4       Text color contains red.
    FOREGROUND_INTENSITY         8       Text color is intensified.
    BACKGROUND_BLUE             10       Background color contains blue.
    BACKGROUND_GREEN            20       Background color contains green.
    BACKGROUND_RED              40       Background color contains red.
    BACKGROUND_INTENSITY        80       Background color is intensified.
    COMMON_LVB_LEADING_BYTE     ??       Leading byte.
    COMMON_LVB_TRAILING_BYTE    ??       Trailing byte.
    COMMON_LVB_GRID_HORIZONTAL  ??       Top horizontal.
    COMMON_LVB_GRID_LVERTICAL   ??       Left vertical.
    COMMON_LVB_GRID_RVERTICAL   ??       Right vertical.
    COMMON_LVB_REVERSE_VIDEO    ??       Reverse foreground and background attributes.
    COMMON_LVB_UNDERSCORE       ??       Underscore.
    The foreground attributes specify the text color.
    The background attributes specify the color used to fill the cell's background. The other attributes are used with DBCS.
    An application can combine the foreground and background constants to achieve
    different colors. For example, the following combination results in bright cyan
    text on a blue background.
    If no background constant is specified, the background is black,
    and if no foreground constant is specified, the text is black.
    For example, the following combination produces black text on a
    white background.
    Each screen buffer character cell stores the color attributes for the colors
    used in drawing the foreground (text) and background of that cell.
    An application can set the color data for each character cell individually,
    storing the data in the Attributes member of the CHAR_INFO structure for each
    cell. The current text attributes of each screen buffer are used for characters
    subsequently written or echoed by the high-level functions.
    An application can use GetConsoleScreenBufferInfo to determine the current text
    attributes of a screen buffer and the SetConsoleTextAttribute function to set
    the character attributes. Changing a screen buffer's attributes does not affect
    the display of characters previously written. These text attributes do not
    affect characters written by the low-level console I/O functions
    (such as the WriteConsoleOutput or WriteConsoleOutputCharacter function),
    which either explicitly specify the attributes for each cell that is written
    or leave the attributes unchanged.
    Font Attributes
    The GetCurrentConsoleFont function retrieves information about the current
    console font. The information stored in the CONSOLE_FONT_INFO structure
    includes the width and height of each character in the font.
    The GetConsoleFontSize function retrieves the size of the font used by the
    specified console screen buffer.
    It's a bit to go through to but actually it's kinda fun. Hope it helps somebody...

Share This Page