简体   繁体   English

RGB 特定控制台文本颜色 C++

[英]RGB Specific Console Text Color C++

I'm trying to set a win32 console application's font color to a specific RGB value like 50, 75, 90 respectively.我正在尝试将 win32 控制台应用程序的字体颜色分别设置为特定的 RGB 值,如 50、75、90。 I've already tried SetConsoleTextAttribute() , but unfortunately it seems to be limited to 0 or 255 for R, G, or B.我已经尝试过SetConsoleTextAttribute() ,但不幸的是,对于 R、G 或 B,它似乎仅限于 0 或 255。

This has to be possible, because in the command prompt properties window you can set the specific color, like so这必须是可能的,因为在命令提示符属性窗口中,您可以设置特定的颜色,如下所示

http://www.yourgamercard.net/screen/i/4a8c57.png

I've searched quite a bit but it seems that the only answer is SetConsoleTextAttribute() .我已经搜索了很多,但似乎唯一的答案是SetConsoleTextAttribute()

You need to use SetConsoleScreenBufferInfoEx to set this, see the ColorTable entry of the CONSOLE_SCREEN_BUFFER_INFOEX struct.您需要使用SetConsoleScreenBufferInfoEx来设置它,请参阅CONSOLE_SCREEN_BUFFER_INFOEX结构的 ColorTable 条目。

Console colors are a two-level process: there's the console attribute, which has four bits each for foreground and background (red, green, blue and intensity), which appears to limit the colors to the basic primary and secondary colors.控制台颜色是一个两级过程:控制台属性,前景和背景各有四位(红色、绿色、蓝色和强度),似乎将颜色限制为基本的主色和辅助色。 But these values are used as indices to the color table, to determine the actual display value.但是这些值被用作颜色表的索引,以确定实际的显示值。 So think of the character attribute 'color' bits as "logical red" etc rather than physical red.因此,将字符属性“颜色”位视为“逻辑红色”等而不是物理红色。 (The value that Character Attribute 'red' maps to is actually RGB red by default, but doesn't have to be.) So you're always limited to 16 indexed colors; (Character Attribute 'red' 映射到的值在默认情况下实际上是 RGB 红色,但不一定是。)所以你总是被限制为 16 种索引颜色; but you can set those to whatever 16 full-RGB colors you want via the ColorTable.但是您可以通过 ColorTable 将它们设置为您想要的任何 16 种全 RGB 颜色。

The strip of colored squares you see in the dialog above is essentially that color table, and lists the colors in their Character Attribute order, the first suqare being 'logical black', and so on.您在上面的对话框中看到的彩色方块带本质上就是那个颜色表,并按照它们的字符属性顺序列出颜色,第一个 suqare 是“逻辑黑色”,依此类推。

Sorry for being a little late to answer but here is the code you desire:很抱歉回答晚了一点,但这里是您想要的代码:

CONSOLE_SCREEN_BUFFER_INFOEX info;
info.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfoEx(hConsole, &info);

info.ColorTable[0] = RGB(0,0,0);
...
info.ColorTable[3] = RGB(135, 206, 235);
...
info.ColorTable[15] = RGB (25,25,25);

SetConsoleScreenBufferInfoEx(hConsole, &info);

With this code you can change color values of all 16 index colors to any RGB color you desire.使用此代码,您可以将所有 16 种索引颜色的颜色值更改为您想要的任何 RGB 颜色。

Than you can print line with your desired color like this:然后你可以像这样用你想要的颜色打印线:

SetConsoleTextAttribute(hConsole, 3);
std::cout << "Hello World!" << std::endl;

And here is my output: My output windows这是我的输出:我的输出窗口

There is a way to make it so the text is completely RGB, but it requires ' SetPixel ' if you make a replica of the text you can then change the RGB values, here is something I made for A, it is hard to do, but I'm making a .h file so everyone can use it, here you go:有一种方法可以使文本完全为 RGB,但它需要“ SetPixel ”,如果您制作文本的副本,则可以更改 RGB 值,这是我为 A 制作的东西,很难做到,但我正在制作一个 .h 文件,以便每个人都可以使用它,给你:

void PrintA(int R, int G, int B)
{
    HWND myconsole = GetConsoleWindow();
    HDC mydc = GetDC(myconsole);
    SetPixel(mydc, i + 0, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 0, i2 + 9, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 1, i2 + 9, RGB(R, G, B));
    SetPixel(mydc, i + 2, i2 + 1, RGB(R, G, B));
    SetPixel(mydc, i + 2, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 2, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 3, i2 + 1, RGB(R, G, B));
    SetPixel(mydc, i + 3, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 3, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 2, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 4, i2 + 9, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 3, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 4, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 5, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 6, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 7, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 8, RGB(R, G, B));
    SetPixel(mydc, i + 5, i2 + 9, RGB(R, G, B));
    i += 8;

    if (i / 80 == 8)
    {
        i = 0;
        i2 += 12;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM