Thread: a simple C question...

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

    a simple C question...

    Can I have font color in C(e.g. blue, red, green, etc...)??
    If I can... how? thx......

  2. #2
    Registered User SavesTheDay's Avatar
    Join Date
    Jan 2002
    Posts
    77
    Well......you can modify your config.sys (maybe it's autoexec.bat...i forget)...and load the DOS based ANSI.SYS file....if the ANSI.SYS is loaded, then there are all new kinds of escape codes you can throw in your printf statements which allow to print in colors (foreground and background) and some other neat things..........without loading ANSI.SYS...i don't know...

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Well, Your question was a bit ambiguous. Did u want the colours of the C-Editor (TC IDE). I guess no, I think you want to know how to change the colours of the font (foreground and background) for the text that you print using printf etc.
    Try this...
    Include CONIO.H header file (small case)

    then use "textcolor(int)" and "textbackground(int)" functions to set the foreground and background colours respectively and then use the "cprintf()" function just like you used printf().

    Regards.
    Help everyone you can

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Well, Your question was a bit ambiguous. Did u want the colours of the C-Editor (TC IDE). I guess no, I think you want to know how to change the colours of the font (foreground and background) for the text that you print using printf etc.
    Try this...
    Include CONIO.H header file (small case)

    then use "textcolor(int)" and "textbackground(int)" functions to set the foreground and background colours respectively and then use the "cprintf()" function just like you used printf().

    Eg:
    #include<conio.h>
    void main()
    {
    textbackground(10);
    clrscr();
    printf("THIS IS WHITE"); /* This is default and always prints in white */
    textcolor(8);
    cprintf("THIS IS ONE COLOUR");
    textcolor(13);
    cprintf("THIS IS ANOTHER COLOUR");
    } /*This is the manner */
    Hope you got my point.
    Regards.
    Help everyone you can

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Sorry... I don't get your point...

    Where are those functions from?
    textbackground(); clrscr(); textcolor();

    I don't understand... would you mind explaining them again.... sorry.... thx....

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    thx!!

    I found out a good code there... but I don't understand....


    Code:
    #include <stdio.h>
    #include <windows.h>  
    
    int main(){
        HANDLE h;
    
        h = GetStdHandle(STD_OUTPUT_HANDLE); 
        SetConsoleTextAttribute(h, FOREGROUND_RED);
        
        printf ("Red font color!\n");
    
        return EXIT_SUCCESS;
    }
    Would anyone tell me how it work? thx.....

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    from the good ol helpfiles....

    The SetConsoleTextAttribute function sets the foreground (text) and background color attributes of characters written to the screen buffer by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsole function. This function affects only text written after the function call.

    BOOL SetConsoleTextAttribute(
    HANDLE hConsoleOutput, // handle to console screen buffer
    WORD wAttributes // text and background colors
    );

    Parameters
    hConsoleOutput
    Handle to a console screen buffer. The handle must have GENERIC_READ access.
    wAttributes
    Specifies the foreground and background color attributes. Any combination of the following values can be specified: FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_INTENSITY, BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_RED, and BACKGROUND_INTENSITY. For example, the following combination of values produces white text on a black background:
    FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE

    Return Values
    If the function succeeds, the return value is nonzero.

    If the function fails, the return value is zero. To get extended error information, call GetLastError.

    Remarks
    To determine the current color attributes of a screen buffer, call the GetConsoleScreenBufferInfo function.

    QuickInfo
    Windows NT: Requires version 3.1 or later.
    Windows: Requires Windows 95 or later.
    Windows CE: Unsupported.
    Header: Declared in wincon.h.
    Import Library: Use kernel32.lib.

    and

    The GetStdHandle function returns a handle for the standard input, standard output, or standard error device.

    HANDLE GetStdHandle(
    DWORD nStdHandle // input, output, or error device
    );

    Parameters
    nStdHandle
    Specifies the device for which to return the handle. This parameter can have one of the following values: Value Meaning
    STD_INPUT_HANDLE Standard input handle
    STD_OUTPUT_HANDLE Standard output handle
    STD_ERROR_HANDLE Standard error handle


    Return Values
    If the function succeeds, the return value is a handle to the specified device.

    If the function fails, the return value is the INVALID_HANDLE_VALUE flag. To get extended error information, call GetLastError.

    Remarks
    Handles returned by GetStdHandle can be used by applications that need to read from or write to the console. When a console is created, the standard input handle is a handle to the console's input buffer, and the standard output and standard error handles are handles of the console's active screen buffer. These handles can be used by the ReadFile and WriteFile functions, or by any of the console functions that access the console input buffer or a screen buffer (for example, the ReadConsoleInput, WriteConsole, or GetConsoleScreenBufferInfo functions).

    All handles returned by this function have GENERIC_READ and GENERIC_WRITE access unless the SetStdHandle function has been used to set a standard handle to be some handle with a lesser access.

    The standard handles of a process may be redirected by a call to SetStdHandle, in which case GetStdHandle returns the redirected handle. If the standard handles have been redirected, you can specify the CONIN$ value in a call to the CreateFile function to get a handle to a console's input buffer. Similarly, you can specify the CONOUT$ value to get a handle to a console's active screen buffer.

    QuickInfo
    Windows NT: Requires version 3.1 or later.
    Windows: Requires Windows 95 or later.
    Windows CE: Unsupported.
    Header: Declared in winbase.h.
    Import Library: Use kernel32.lib.
    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

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    4
    vsriharsha, Thank you.

    I just spent more than a week scouring books and the net for an answer to this question and your chunk of code worked perfectly.


  10. #10
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up Any Time Buddy

    Glad 2 note that u got what ur looking for.

  11. #11
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356

    Wink just a tip

    If you are not using borland then instead of using conio.h use conio.c


    Data.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM