Win32 Color in Console Mode

Started by
8 comments, last by randomDecay 22 years, 6 months ago
Hi. I want to use colors in my program for my text and fill the entire portion of the console with a specific color. Can any one help me out!!
Advertisement
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.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Cool, it works. How would I fill the whole window with a color though?
This is a good place to start. I suggest you use WriteConsoleAttribute.

"A society without religion is like a crazed psychopath without a loaded .45"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
awesome, thanks guys!
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
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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.
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"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Ahh, cool. Thanks for the tip. We learn something new every day
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
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"
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]

This topic is closed to new replies.

Advertisement