有没有办法在 Windows 控制台中获得更多颜色(c++)?

发布于 2025-01-14 07:21:04 字数 445 浏览 3 评论 0原文

有没有办法在 Windows 控制台(C++)中获得更多颜色?

我所说的“更多”是指 RGB 颜色。我尝试过:

CONSOLE_SCREEN_BUFFER_INFOEX info;
info.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleScreenBufferInfoEx(hcon, &info);

info.ColorTable[0] = 0x505050;
info.ColorTable[1] = 0xcccccc;

SetConsoleScreenBufferInfoEx(hcon, &info);

SetConsoleTextAttribute(hcon, 01);

但我一次只能使用16种颜色。

Is there a way to get more colors in Windows console (c++)?

By "more", I mean RGB colors. I have tried:

CONSOLE_SCREEN_BUFFER_INFOEX info;
info.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);

GetConsoleScreenBufferInfoEx(hcon, &info);

info.ColorTable[0] = 0x505050;
info.ColorTable[1] = 0xcccccc;

SetConsoleScreenBufferInfoEx(hcon, &info);

SetConsoleTextAttribute(hcon, 01);

but I can use only 16 colors at one time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2025-01-21 07:21:04

来自控制台 API 的一些文档,特别是与 扩展颜色的虚拟终端序列

某些虚拟终端模拟器支持的颜色调色板大于 Windows 控制台提供的 16 种颜色。对于这些扩展颜色,Windows 控制台将从现有的 16 色表中选择最接近的合适颜色进行显示。

基于此,我假设 Windows 控制台不支持超过 16 种颜色。如果我不得不猜测的话,这是与假设只有 16 种颜色的程序交互的向后兼容性限制。

如果您想要更多,最好不要使用 Windows 控制台。如果这不可能,那么您就只能得到总共 16 个。

From some of the documentation on the console API, specifically relating to Virtual Terminal Sequences for Extended Color:

Some virtual terminal emulators support a palette of colors greater than the 16 colors provided by the Windows Console. For these extended colors, the Windows Console will choose the nearest appropriate color from the existing 16 color table for display.

Based on this, I'd make the assumption that the Windows Console doesn't support anything more than 16 colors. If I had to guess, this is a backwards-compatibility restriction to interact with programs that assumed there were only ever 16 colors.

If you want more, you'd be best served by not using the Windows Console. If that's not possible, then you're stuck with 16 total.

老娘不死你永远是小三 2025-01-21 07:21:04

您必须使用 VT 颜色序列。

像这样启用:

    GetConsoleMode(stdHandle, &mode);
    SetConsoleMode(stdHandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

此示例显示了它可以执行的操作:

char CSI[3] = { 0x1b, '[', 0 };
char Logactivity[3] = { 0x1b, 'M', 0 };

void test_colors()
{
    for (int i = 0; i < 256; i += 128) {
        for (int j = 0; j < 128; j++) {
            std::cout << CSI << "48;5;" << std::to_string(j + i) << "m ";
        }
        std::cout << std::endl;
    }
    for (int i = 0; i < 256; i += 128) {
        for (int j = 0; j < 128; j++) {
            std::cout << CSI << "48;2;" << "0;0;" << std::to_string(j + i) << "m ";
        }
        std::cout << std::endl;
    }
}

在此处输入图像描述

You have to the use VT color sequences.

Enable like so:

    GetConsoleMode(stdHandle, &mode);
    SetConsoleMode(stdHandle, mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);

This example, shows what it can do:

char CSI[3] = { 0x1b, '[', 0 };
char Logactivity[3] = { 0x1b, 'M', 0 };

void test_colors()
{
    for (int i = 0; i < 256; i += 128) {
        for (int j = 0; j < 128; j++) {
            std::cout << CSI << "48;5;" << std::to_string(j + i) << "m ";
        }
        std::cout << std::endl;
    }
    for (int i = 0; i < 256; i += 128) {
        for (int j = 0; j < 128; j++) {
            std::cout << CSI << "48;2;" << "0;0;" << std::to_string(j + i) << "m ";
        }
        std::cout << std::endl;
    }
}

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文