更改整个控制台背景色(Win32 C ++)

| 如何更改整个控制台的背景颜色?我尝试了“ 0”,它只更改了新文本的背景色。 当出现严重错误时,我实际上希望整个控制台变成红色。 感谢所有尝试提供帮助的人。     
已邀请:

bab

        尝试类似的东西:
system(\"color c2\");
    
        我认为
FillConsoleOutputAttribute
函数将满足您的需求。将其设置为控制台的起始坐标,并将
nLength
设置为控制台中的字符数(
width * length
)。
BOOL WINAPI FillConsoleOutputAttribute(
  __in   HANDLE hConsoleOutput,
  __in   WORD wAttribute,
  __in   DWORD nLength,
  __in   COORD dwWriteCoord,
  __out  LPDWORD lpNumberOfAttrsWritten
);
    
        我知道这是一个老问题,但是这段代码呢:
#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     
        这对我有用。通过一次更改每个控制台字符单元,它可以改变背景色而不会弄乱已经显示的文本的前景色。您将需要获取控制台输出缓冲区的句柄,我相信这是通过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);
    }
}
    
        可以做到这一点,并可以使用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;
    }
    
        我在这里有一个肮脏的方法,但是给出您真正想要的。
  #include <windows.h>
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(hConsole,30);
  system(\"CLS\");
安慰     
        
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.
/*
    

要回复问题请先登录注册