本文介绍了C ++ Win32控制台颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有点如何在Win32 C ++控制台中做颜色。但它不是真的效率。例如:

I know a bit how to do colors in Win32 C++ console. But it's not really efficiënt. 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 );

效果很好,但颜色不多。此外,FOREGROUND_RED是深红色/

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

所以我想问,是不是有一种方式像CLR属性控制台:: ForegroundColor设置,所以你可以使用任何

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?

推荐答案

控制台仅支持16种颜色,它们是通过组合四个值创建的,如下所示可能有灰色/ darkgray困惑,但你得到的想法):

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,
  };
}

这篇关于C ++ Win32控制台颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 23:12