Is there any way?

This is a discussion on Is there any way? within the C++ Programming forums, part of the General Programming Boards category; I was just wondering if there is anyway to change the color of the text in a "consol application'? If ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    7

    Is there any way?

    I was just wondering if there is anyway to change the color of the text in a "consol application'?

    If there is please show me an example of how I would code that.

    Thank you.

  2. #2
    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

  3. #3
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    Originally posted by Stoned_Coder

    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.
    didnt check to see if this is an old post but anyway.

    which parameter is it talking about?
    lpFileName ???

    nm should have tried before i posted.
    anyways i can use the debugger that came with dev-c++ now.
    still cant get cout or printf to work tho
    Code:
        m_Screen = CreateFile("CONOUT$",       // open MYFILE.TXT
                    GENERIC_WRITE,             // open for reading
                    FILE_SHARE_WRITE,          // share for reading
                    NULL,                      // no security
                    OPEN_EXISTING,             // existing file only
                    FILE_ATTRIBUTE_NORMAL,     // normal file
                    NULL);                     // no attr. template
    
        m_Keyboard = CreateFile("CONIN$",      // open MYFILE.TXT
                    GENERIC_READ,              // open for reading
                    FILE_SHARE_READ,           // share for reading
                    NULL,                      // no security
                    OPEN_EXISTING,             // existing file only
                    FILE_ATTRIBUTE_NORMAL,     // normal file
                    NULL);                     // no attr. template
        
        SetStdHandle( STD_OUTPUT_HANDLE, m_Screen);
        SetStdHandle( STD_INPUT_HANDLE, m_Keyboard);
    Last edited by Nor; 05-21-2003 at 07:27 PM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

Popular pages Recent additions subscribe to a feed

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