Archived

This topic is now archived and is closed to further replies.

randomDecay

Win32 Color in Console Mode

Recommended Posts

To output red text:
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(consoleHandle, FOREGROUND_RED);
To output blue or green text, replace FOREGROUND_RED with FOREGROUND_GREEN or FOREGROUND_BLUE. You can also combine colors by using something like FOREGROUND_RED | FOREGROUND_BLUE to get purple.

Share this post


Link to post
Share on other sites
I think this should do it .. fills the window with red ...
    
CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD numCharactersWritten;
COORD consoleCoords = {0, 0};
SetConsoleTextAttribute(consoleHandle, BACKGROUND_RED);
GetConsoleScreenBufferInfo(consoleHandle, &consoleInfo);
FillConsoleOutputCharacter(consoleHandle, ' ', consoleInfo.dwSize.X * consoleInfo.dwSize.Y,
consoleCoords, &numCharactersWritten);
FillConsoleOutputAttribute(consoleHandle, consoleInfo.wAttributes, consoleInfo.dwSize.X * consoleInfo.dwSize.Y,
consoleCoords, &numCharactersWritten);


Edited by - Martee on October 19, 2001 7:48:23 PM

Share this post


Link to post
Share on other sites
One more quick q...

I have one line at the top of the window with a blue background and white text for the player''s score and name. How would I keep that there for the entire program, and have it stay at the top while the rest of the program keeps going below it.

Share this post


Link to post
Share on other sites
Martee: Using WriteConsoleAttribute would let you set the color independently, without having to write to the cells.

"A society without religion is like a crazed psychopath without a loaded .45"

Share this post


Link to post
Share on other sites
quote:
Original post by Martee
Ahh, cool. Thanks for the tip. We learn something new every day


No point in getting out of bed otherwise

"A society without religion is like a crazed psychopath without a loaded .45"

Share this post


Link to post
Share on other sites