Changing text color

Hi all,

I want to change ONLY the foreground color text of my console aplication, not the background text color nor the console background color. In Other words: I want to keep previous colors like they are except the foreground text color.

How can I do that?

I'm trying something like the following, but I think I'm misusing the consoleInfo.wAttributes:

1
2
3
4
5
6
7
8
9
void setTextColor( const int textColor )
{
	CONSOLE_SCREEN_BUFFER_INFO consoleInfo;

	GetConsoleScreenBufferInfo( GetStdHandle(STD_OUTPUT_HANDLE), &consoleInfo );

	SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), 
		textColor | consoleInfo.wAttributes );
}


Thanks in advance
Last edited on
* Disch tries really hard to resist making "why are you doing this kind of thing in the console -- this isn't 1989 anymore" style remarks *

* Disch fails *

Sadly I can't provide any useful information for you. Sorry =(
The lower four bits represent the foreground color; the upper four bits the background color.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void setTextColor( const int textColor )
{
    HANDLE                     hStdOut;
    CONSOLE_SCREEN_BUFFER_INFO consoleInfo;

    hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    if (hStdOut == INVALID_HANDLE) return;

    if (!GetConsoleScreenBufferInfo( hStdOut, &consoleInfo )) return;

    SetConsoleTextAttribute(
        hStdOut,
        (textColor & 0x0F) | (consoleInfo.wAttributes & 0xF0)
        );
}

Hope this helps.
Last edited on
thank you!
If your using Windows then, well I remember using system("color <value>").

http://www.ss64.com/nt/color.html

there's a whole list of values. Very simple and less code.
*sigh* ...And something people should be fired for doing. Don't use system().
Topic archived. No new replies allowed.