How to make colored output

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

  1. jan1024188

    jan1024188 New Member

    Joined:
    Dec 31, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    3
    Trophy Points:
    0
  3. HowardL

    HowardL New Member

    Joined:
    Aug 5, 2007
    Messages:
    15
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    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):
    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++) {
        Pos.Y++;
        SetConsoleCursorPosition(hOut, Pos);
        SetConsoleTextAttribute(hOut, i);
        printf("zelena 0x%02x", i);
      }
    
      Pos.Y +=2;
      SetConsoleCursorPosition(hOut, Pos);
      SetConsoleTextAttribute(hOut, 0x20);
      printf("zelena 0x20");
    
      Pos.Y++;
      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!)
    http://msdn2.microsoft.com/en-us/library/ms682010.aspx
    ------------
    Specifically speaking about attributes:
    http://msdn2.microsoft.com/en-us/library/ms682088.aspx
    
    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.
    
    FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_BLUE
    
    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.
    
    BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
    
    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...
    Howard;
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice