Coulor Of Text....

This is a discussion on Coulor Of Text.... within the C Programming forums, part of the General Programming Boards category; Anyone know how to change the coulor of text in a C program, and which header file i should use...any ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    44

    Coulor Of Text....

    Anyone know how to change the coulor of text in a C program, and which header file i should use...any help is greatly recieved.
    Dangerous Dave

  2. #2
    Unregistered
    Guest
    #include <stdio.h>
    #include <time.h>
    #include <conio.c>
    int main (void)
    {
    int counter = 0;
    int x;
    srand( (unsigned)time( NULL ) );

    {
    printf ("\n\t\tThis Text is Displayed in Standard color\n");
    textcolor(BLUE); /*changes text color*/
    printf ("\n\t\tTen Random Numbers within range\n");
    do
    {
    x = (rand() %50) + 51;/* limits rand range between 50-100*/
    textcolor(YELLOW); /*changes text color*/
    printf ("\n\t\t\t\t%d\n ",x);
    counter++;
    x=0;
    }
    while ( counter<10);/* displays 10 random numbers */

    }
    textcolor(LIGHTGRAY); /*returns screen color*/
    printf ("\n\t\tPress Return/Enter key to close");/* holds display untill key press */
    getch();
    return(0);
    }




    sall

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    44
    Wimdows 98 and dev-c++, any more help?
    Dangerous Dave

  4. #4
    0x01
    Join Date
    Sep 2001
    Posts
    88
    #include "windows.h" // always include this for SetConsoleTextAttribute()
    #include "stdio.h"
    #include "stdlib.h"

    int main()
    {
    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED);
    printf("Hello, world.. this text should be red\n");

    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE);
    printf("Hello, world.. this text should be BLUE\n");

    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
    printf("Hello, world.. this text should be GREEN\n");

    // If you change the FOREGROUND part to BACKGROUND then the back ground of the text will be any of the chosen colors.

    // for instances:
    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), BACKGROUND_RED);
    printf("Hello, world.. this text should be RED\n");

    // you can also mix the colors like this
    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE);
    printf("Hello, world.. this text should be DEFAULT or greyish white\n");

    // You can also add brightness to the colors that you use by adding this FOREGROUND_INTENSITY, this could be changed to BACKGROUND_INTENSITY as well
    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
    printf("Hello, world.. this text should be WHITE\n");

    // Or if you know what number the color is that you want then try this
    SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), 0); // change the 0 to any other number to get a different color
    printf("Hello, world.. this text should be BLACK\n"); // you probably won't see this since it is black

    return 0;
    }
    Last edited by knave; 11-24-2001 at 06:16 PM.

  5. #5
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    use code tags please... and, to make this post not just a rant for board use... i'll say that you can tinker with b800 to manually change your text output... however this is conventional memory...
    hasafraggin shizigishin oppashigger...

  6. #6
    Unregistered
    Guest

    Wink

    Hi Knave
    I have been playing around with your code, very helpfull,I am intrested in the
    part of the code were digits are used to alter the color,5,9,20,15,etc,etc.
    Can you point me to a link that has the full list of number codes with there color/highlight colors
    sall

  7. #7
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    dont use numbers.... use the defined constants instead. You can get 16 different colours by oring them together as knave has shown you. You only need to use 0 for black.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  8. #8
    Unregistered
    Guest
    Hi,
    how do i set these colors under Linux/UNIX using gcc instead of Windows and dev cpp?

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    i have never programmed for unix/linux but i believe there is a header curses.h that has some functions in it to do that. not 100% sure tho.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Unregistered
    Guest
    Hi,
    when i compile the first code in this thread alone, it works fine... but everytime i try to just insert-

    Code:
    textcolor(BLUE); /*changes text color*/ 
    printf ("\n\t\tTen Random Numbers within range\n");
    -in another source code (of course i always include conio.h, too), i get the following linker error:

    C:\WINDOWS\TEMP\ccDzyagb.o(.text+0x85a):fp.cpp: undefined reference to `textcolor'

    What the heck is this supposed to mean?? umm.. ok, it means that there is an "undefined reference", but why do i get this error with a code that works perfectly alone? What could be wrong and how do i make it compile?
    --
    (Dev Cpp, Windows 98)

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    It means you dont have a textcolor() function... instead use the way that knave has shown you.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  12. #12
    Unregistered
    Guest
    Yeah, it has to mean something like that :-). but how can this be possible if i include ///!!!! CONIO.H !!!!////, which has this function? If i compile these two lines quoted above alone (in main, of course), it works and displays this line in blue. But if i insert it in any other source code, i get this linker error... Nothing different, just other code besides these lines. Please help..

  13. #13
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    that example included conio.c instead of conio.h

    This is unusual behaviour BUT it may be something that is specific to dev-c. If includidng conio.c instead of conio.h sorts the problem out then so be it but dont expect your code to compile on other compilers without changes.

    Why not write your own textcolor function. Its quite easy to do for windows console.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  14. #14
    Unregistered
    Guest
    Great, thanks! I think would have never found this mistake... Well i use #ifdef's so that it compiles with textcolor() under win and with the standard ansi codes for printing coloured text under *nix (they don't work with dev c++ :/).

  15. #15
    Unregistered
    Guest
    Save these two files in your project folder and write #include "conio_simple.c" in your program. This was tested on one turboc program while porting it to dev-c++, so it doesn't define all borland-specific functions and not these what are not conio.h funtions (int86 etc).

    /*
    conio_simple.h
    Console i/o functions for mingw, tested on 1 turboc program.
    Created by tkorrovi <tkorrovi@altavista.net> on 2002/02/26.
    Offered for use in the public domain without any warranty.
    */

    enum COLORS {BLACK, BLUE, GREEN, CYAN, RED, MAGENTA, BROWN, LIGHTGRAY,
    DARKGRAY, LIGHTBLUE, LIGHTGREEN, LIGHTCYAN, LIGHTRED, LIGHTMAGENTA,
    YELLOW, WHITE};
    int directvideo;

    extern "C"
    {
    char* _cgets (char* szBuffer);
    int _cprintf (const char* szFormat, ...);
    int _cputs (const char* szString);
    int _cscanf (char* szFormat, ...);
    int getch ();
    int getche ();
    int kbhit ();
    int putch (int cPut);
    int ungetch (int cUnget);
    }

    #define _NOCURSOR 100
    #define _SOLIDCURSOR 99
    #define _NORMALCURSOR 10
    #define cgets _cgets
    #define cprintf _cprintf
    #define cputs _cputs
    #define cscanf _cscanf

    VOID gettext (INT x1, INT y1, INT x2, INT y2, LPSTR s);
    VOID puttext (INT x1, INT y1, INT x2, INT y2, LPSTR s);
    void _setcursortype (int type);
    void gotoxy (int x, int y);
    void clrscr ();
    void textcolor (int color);
    void textbackground (int color);
    VOID outport (INT a, INT b);


    /*
    conio_simple.c
    Console i/o functions for mingw, tested on 1 turboc program.
    Created by tkorrovi <tkorrovi@altavista.net> on 2002/02/26.
    Offered for use in the public domain without any warranty.
    */

    #include <windows.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "conio_simple.h"

    int __BACKGROUND = BLACK, __FOREGROUND = LIGHTGRAY;

    VOID gettext (INT x1, INT y1, INT x2, INT y2, LPSTR s)
    {
    INT i, j, n;
    SMALL_RECT r;
    CHAR_INFO buffer [25] [80];
    r = (SMALL_RECT) {x1 - 1, y1 - 1, x2 - 1, y2 - 1};
    ReadConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
    (_CHAR_INFO *) buffer,
    (COORD) {80, 25}, (COORD) {0, 0}, &r);
    lstrcpy (s, "");
    for (i = n = 0; i <= y2 - y1; i++)
    for (j = 0; j <= x2 - x1; j++)
    {
    s [n++] = buffer [i] [j].Char.AsciiChar;
    s [n++] = buffer [i] [j].Attributes;
    }
    s [n] = 0;
    }

    VOID puttext (INT x1, INT y1, INT x2, INT y2, LPSTR s)
    {
    INT i, j, n;
    SMALL_RECT r;
    CHAR_INFO buffer [25] [80];
    for (i = n = 0; i <= y2 - y1; i++)
    for (j = 0; j <= x2 - x1 && s [n] != 0; j++)
    {
    buffer [i] [j].Char.AsciiChar = s [n++];
    buffer [i] [j].Attributes = s [n++];
    }
    r = (SMALL_RECT) {x1 - 1, y1 - 1, x2 - 1, y2 - 1};
    WriteConsoleOutput (GetStdHandle (STD_OUTPUT_HANDLE),
    (_CHAR_INFO *) buffer,
    (COORD) {80, 25}, (COORD) {0, 0}, &r);
    }

    void _setcursortype (int type)
    {
    CONSOLE_CURSOR_INFO Info;
    Info.dwSize = type;
    SetConsoleCursorInfo (GetStdHandle (STD_OUTPUT_HANDLE), &Info);
    }

    void gotoxy (int x, int y)
    {
    SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE),
    (COORD) {x - 1, y - 1});
    }

    void clrscr ()
    {
    DWORD written;
    FillConsoleOutputAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
    __FOREGROUND + (__BACKGROUND << 4), 2000, (COORD) {0, 0}, &written);
    FillConsoleOutputCharacter (GetStdHandle (STD_OUTPUT_HANDLE), ' ',
    2000, (COORD) {0, 0}, &written);
    gotoxy (1, 1);
    }

    void textcolor (int color)
    {
    __FOREGROUND = color;
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
    color + (__BACKGROUND << 4));
    }

    void textbackground (int color)
    {
    __BACKGROUND = color;
    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
    __FOREGROUND + (color << 4));
    }

    VOID outport (INT a, INT b) {}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 10:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21