Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Win32 Console Color

I know a bit how to do colors in Win32 C++ console. But it's not really efficient. For example:

 SYSTEM("color 01")

Slows down a lot on your process. Also:

 HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
 WORD wOldColorAttrs;
 CONSOLE_SCREEN_BUFFER_INFO csbiInfo;

 /*
  * First save the current color information
  */

 GetConsoleScreenBufferInfo(h, &csbiInfo);
 wOldColorAttrs = csbiInfo.wAttributes;

 /*
  * Set the new color information
  */

 SetConsoleTextAttribute ( h, FOREGROUND_RED );

Works great, but it doesn't have much colors. Also, FOREGROUND_RED is dark-red.

So what I want to ask, isn't there a way like CLR property Console::ForegroundColor set, so you can use any color from the ConsoleColor enum?

like image 414
x_metal_pride_diamond_x Avatar asked Jun 15 '13 15:06

x_metal_pride_diamond_x


1 Answers

The console only supports 16 colors, which are created by combining the four values as follows (I might have got the gray/darkgray confused, but you get the idea):

namespace ConsoleForeground
{
  enum {
    BLACK             = 0,
    DARKBLUE          = FOREGROUND_BLUE,
    DARKGREEN         = FOREGROUND_GREEN,
    DARKCYAN          = FOREGROUND_GREEN | FOREGROUND_BLUE,
    DARKRED           = FOREGROUND_RED,
    DARKMAGENTA       = FOREGROUND_RED | FOREGROUND_BLUE,
    DARKYELLOW        = FOREGROUND_RED | FOREGROUND_GREEN,
    DARKGRAY          = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
    GRAY              = FOREGROUND_INTENSITY,
    BLUE              = FOREGROUND_INTENSITY | FOREGROUND_BLUE,
    GREEN             = FOREGROUND_INTENSITY | FOREGROUND_GREEN,
    CYAN              = FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE,
    RED               = FOREGROUND_INTENSITY | FOREGROUND_RED,
    MAGENTA           = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE,
    YELLOW            = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN,
    WHITE             = FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE,
  };
}
like image 97
riv Avatar answered Sep 21 '22 13:09

riv