How to change text colors in C?

Discussion in 'C' started by techme, Mar 27, 2010.

  1. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    any body can help me to change text colour? i am completly dizzy if you can help me!please reply the source code!i hope for it!
     
  2. pankaj.sea

    pankaj.sea New Member

    Joined:
    Apr 6, 2009
    Messages:
    461
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    Web Developer
    Location:
    Kolkata
    Home Page:
    http://ipankaj.net
    Here is a small c language program that will demonstrate how you can blink text in c language. you need to use a function textattr() which is present in IO library conio.h.



    The exact syntax of the function is void textattr(int newattr);

    This function enables you to set the color of the text as well as the color of the background of that text.



    So when you set the attribute of using textattr() function it will then format any output characters appearing on the screen after you call this funtion.



    Here is a table with all the values you can specify while calling textattr function.



    7 6 5 4 3 2 1 0

    B b b b F F F F



    F - these four bits define the text color. As you can have 16colors in turbo c.

    b - the combinations of these three bit sets the background color of the text.

    B - its the bit to blink the text. The text will blink if this bit is set to 1, otherwise the text will not blink.



    So assume that you want to blink the text in c language with black background and white text color. You will call textattr function as

    textattr(143);



    binary of 143 = 10001111

    which turns the blink bit to 1, text color to white and background color to black.



    Or the most easy way is to use the color constants defined in conio.h.

    So the new syntax to blink the text in c programming language would be



    textattr(BLINK+BLACK+WHITE);



    There are some limitations on foreground and background colors, like you can not use white color as the background.



    Black, Blue, Green, Cyan, Red, Magenta, Brown, LightGray they all can be used both as text color and background colors.



    DarkGray, LightBlue, LightCyan, LightGreen, LightRed, LikeMagenta, Yellow, White can only be used as text colors.

    Code:
    #include <conio.h>
    #include <process.h></DIV>
    <DIV>void main(void)
    {
       int i;</DIV>
    <DIV>   clrscr();
       for (i=0; i<9; i++)
       {
           textattr(i + ((i+1) << 4));
           cprintf("This is a test\r\n");
       }</DIV>
    <DIV>   getch();
    }
    
    Try this!
     
  3. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    The information was useful!
     
  4. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Thanks Pankaj!
    :) :) :)
     

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