May 2, 2009 at 8:40pm UTC
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 May 2, 2009 at 8:40pm UTC
May 2, 2009 at 9:00pm UTC
* 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 =(
May 2, 2009 at 9:35pm UTC
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 May 2, 2009 at 9:36pm UTC
May 3, 2009 at 5:33am UTC
*sigh* ...And something people should be fired for doing. Don't use system().