【问题标题】:Change entire console background color (Win32 C++)更改整个控制台背景颜色 (Win32 C++)
【发布时间】:2011-09-21 14:26:01
【问题描述】:

如何更改整个控制台的背景颜色?我试过SetConsoleTextAttribute,它只会改变新文本的背景颜色。

当出现严重错误时,我实际上希望整个控制台变红。

感谢所有试图提供帮助的人。

【问题讨论】:

  • 听起来像是easy job in C#,但在 C++ 中.. :|快速谷歌搜索没有找到任何东西。也许只是将文本背景设置为红色并打印整个字符数组,主要是空格,您的文本在里面,也在红色背景上?我认为这可以作为一种解决方法。
  • 这在 Windows 命令语言中是微不足道的:color 4f,仅此而已。 :-)
  • @Alf:这意味着这可以工作:system("cmd /c \"color 4F\"")
  • @Loadmaster 效果很好。我知道它通常不推荐,不便携等,但我认为这是我目前唯一的选择。除非我能弄清楚如何在 C++ 中做与 COLOR 相同的事情(我确信这是可能的)。

标签: c++ windows console background-color


【解决方案1】:

我认为FillConsoleOutputAttribute 函数可以满足您的需求。设置为控制台起始坐标,nLength设置为控制台字符数(width * length)。

BOOL WINAPI FillConsoleOutputAttribute(
  __in   HANDLE hConsoleOutput,
  __in   WORD wAttribute,
  __in   DWORD nLength,
  __in   COORD dwWriteCoord,
  __out  LPDWORD lpNumberOfAttrsWritten
);

【讨论】:

  • 非常接近,但是已经包含字符的单元格保持之前的背景颜色。
  • 猜猜我的 Win32/Console 有点生锈了。你可以试试WriteConsoleOutputAttribute 吗?这可能就是您正在寻找的。​​span>
【解决方案2】:

尝试类似:

system("color c2");

【讨论】:

  • +1 简单但有效,即使使用system 调用速度较慢。
  • 我最终选择了这个。我会更喜欢 API 函数,但这很简单而且效果很好。
  • 不工作!它改变文本颜色和文本背景颜色而不是整个控制台的颜色
【解决方案3】:

这对我有用。它通过一次一个更改每个控制台字符单元格来更改背景颜色,而不会弄乱已显示文本的前景色。您需要获取控制台输出缓冲区的句柄,我相信这是使用 GetStdHandle() 完成的。

DWORD written = 0;
COORD writeCoord = {0};
WORD attribute;
for (int y = 0; y < consoleBufferLength; y++)     // rows
{
    for (int x = 0; x < consoleBufferWidth; x++)  // columns
    {
        writeCoord.X = x; writeCoord.Y = y;
        ReadConsoleOutputAttribute(consoleOutputHandle, &attribute, 1, writeCoord, &written);
        attribute &= 0xFF0F;  // zero the background color
        attribute |= 12 << 4; // change the background color to red
        FillConsoleOutputAttribute(consoleOutputHandle, attribute, 1, writeCoord, &written);
    }
}

【讨论】:

    【解决方案4】:

    我知道这是一个老问题,但是这段代码怎么样:

    #include <windows.h>
    #include <iostream>
    
    
    VOID WINAPI SetConsoleColors(WORD attribs);
    
    
    int main() {
    
        SetConsoleColors(BACKGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
    
        std::cout << "Hello, world!" << std::endl;
        std::cin.get();
    
        return 0;
    }
    
    
    VOID WINAPI SetConsoleColors(WORD attribs) {
        HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
    
        CONSOLE_SCREEN_BUFFER_INFOEX cbi;
        cbi.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
        GetConsoleScreenBufferInfoEx(hOutput, &cbi);
        cbi.wAttributes = attribs;
        SetConsoleScreenBufferInfoEx(hOutput, &cbi);
    }
    

    据我所知,这段代码应该可以在 Windows Vista 和更高版本上运行。顺便说一下,这段代码是根据这篇文章(主要是文章上的出处):http://cecilsunkure.blogspot.fi/2011/12/windows-console-game-set-custom-color.html

    【讨论】:

    • 由于某种原因,这个解决方案导致我的控制台窗口每次颜色变化都会缩小
    【解决方案5】:

    可以通过SetConsoleScreenBufferInfoEx 将整个背景设置为所需的颜色。下面的代码不应与之前的控制台输出混淆,尤其是在它使用颜色的情况下:

     #include "Windows.h"
    
        void FlashConsoleBackgroundColor(int cntFlashes, int flashInterval_ms, COLORREF color)
        {
    
            CONSOLE_SCREEN_BUFFER_INFOEX sbInfoEx;
            sbInfoEx.cbSize = sizeof(CONSOLE_SCREEN_BUFFER_INFOEX);
    
            HANDLE consoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
            GetConsoleScreenBufferInfoEx(consoleOut, &sbInfoEx);
    
            COLORREF storedBG = sbInfoEx.ColorTable[0];
    
            for (int i = 0; i < cntFlashes; ++i)
            {
                //-- set BG color
                Sleep(flashInterval_ms);
                sbInfoEx.ColorTable[0] = color;
                SetConsoleScreenBufferInfoEx(consoleOut, &sbInfoEx);
    
                //-- restore previous color
                Sleep(flashInterval_ms);
                sbInfoEx.ColorTable[0] = storedBG;
                SetConsoleScreenBufferInfoEx(consoleOut, &sbInfoEx);
            }
        }
    
        int main()
        {
    
            printf("Flashing console BG: RED");
            FlashConsoleBackgroundColor(20, 50, RGB(255, 0, 0));
    
            printf("\rFlashing console BG: ORANGE\n");
            FlashConsoleBackgroundColor(10, 100, RGB(255, 105, 0));
    
            return 0;
        }
    

    【讨论】:

    • 这个答案与上面的@MaGetzUb 的答案有何不同?他们都展示了如何使用 GetConsoleScreenBufferInfoExSetConsoleScreenBufferInfoEx 只有你的答案似乎是不必要的复杂。
    • 实际上,我的代码虽然看起来很相似,但在功能上却大不相同。 @MaGetzUb 的代码更改了与最后打印的行具有相同属性的所有字符的前景色和背景色。如果您在此之前没有使用过 SetConsoleTextAttribute,那么整个背景都会发生变化。但是在之前添加:SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x96); printf("Some previous console output\n"); ..它失败了。我的示例仅更改一种颜色,即背景颜色,无论之前打印什么。此外,您还可以将其设置为自定义 RGB。
    【解决方案6】:

    我在这里有一个肮脏的方式,但给出了你真正想要的。

      #include <windows.h>
      hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(hConsole,30);
      system("CLS");
    

    console

    【讨论】:

      【解决方案7】:
      HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);
      SetConsoleTextAttribute(out, 0x9 | 0x70); 
      // text color from 0x1-0x9
      // text background color from 0x10-0x90   
      system("color d1");
      /*
      Sets the default console foreground and background colors     
      COLOR [attr]      
      attr        Specifies color attribute of console output       
      Color attributes are specified by TWO hex digits -- the first
      corresponds to the background; the second the foreground.  Each digit
      can be any of the following values:       
                  0 = Black       8 = Gray
                  1 = Blue        9 = Light Blue
                  2 = Green       A = Light Green
                  3 = Aqua        B = Light Aqua
                  4 = Red         C = Light Red
                  5 = Purple      D = Light Purple
                  6 = Yellow      E = Light Yellow
                  7 = White       F = Bright White
      If no argument is given, this command restores the color to what it was
      when CMD.EXE started.  This value either comes from the current console
      window, the /T command line switch or from the DefaultColor registry
      value.       
      The COLOR command sets ERRORLEVEL to 1 if an attempt is made to execute
      the COLOR command with a foreground and background color that are the
      same.
      /*
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-01
        • 2019-08-29
        • 2013-06-12
        • 2011-11-23
        • 1970-01-01
        • 2017-11-18
        • 1970-01-01
        相关资源
        最近更新 更多