Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 更改整个控制台的背景色(Win32 C+;+;)_C++_Windows_Console_Background Color - Fatal编程技术网

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

C++ 更改整个控制台的背景色(Win32 C+;+;),c++,windows,console,background-color,C++,Windows,Console,Background Color,如何更改整个控制台的背景色?我尝试了SetConsoleTextAttribute,它只会更改新文本的背景色 当出现严重错误时,我实际上希望整个控制台变为红色 感谢所有试图提供帮助的人。我认为该功能可以满足您的需要。将其设置为控制台的起始坐标,并将nLength设置为控制台中的字符数(width*length) 尝试以下方法: system("color c2"); 这对我有用。它通过改变每个控制台字符单元(一次一个)来改变背景色,而不会弄乱已显示文本的前景色。您需要获得控制台输出缓冲区的句柄

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

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

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

我认为该功能可以满足您的需要。将其设置为控制台的起始坐标,并将
nLength
设置为控制台中的字符数(
width*length

尝试以下方法:

system("color c2");

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

DWORD write=0;
COORD WriteCord={0};
词属性;
对于(int y=0;y属性|=12我知道这是一个老问题,但这段代码如何:

#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);
}
#包括
#包括
VOID WINAPI SetConsoleColors(单词attribs);
int main(){
设置控制台颜色(背景为蓝色、前景为红色、前景为强度);

std::cout可以这样做,整个背景可以设置为所需的颜色。下面的代码不应与以前的控制台输出混淆,特别是如果它使用颜色:

 #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;
    }
#包括“Windows.h”
无效FlashConsoleBackgroundColor(int-CntFlashs、int-flashInterval\u ms、COLORREF-color)
{
控制台(屏幕)缓冲区(INFOEX sbinfo);;
sbinfo.cbSize=sizeof(控制台屏幕缓冲区信息);
HANDLE consoleOut=GetStdHandle(标准输出句柄);
GetConsoleScreenBufferInfoEx(consoleOut和sbInfoEx);
COLORREF storedBG=sbInfoEx.ColorTable[0];
对于(int i=0;i<1;+i)
{
//--设置背景颜色
睡眠时间(闪光间隔μms);
sbinfo.ColorTable[0]=颜色;
SetConsoleScreenBufferInfoEx(consoleOut和sbInfoEx);
//--恢复以前的颜色
睡眠时间(闪光间隔μms);
sbinfo.ColorTable[0]=storedBG;
SetConsoleScreenBufferInfoEx(consoleOut和sbInfoEx);
}
}
int main()
{
printf(“闪烁控制台背景:红色”);
FlashConsoleBackgroundColor(20,50,RGB(255,0,0));
printf(“\rFlashing console BG:ORANGE\n”);
FlashConsoleBackgroundColor(10100,RGB(255,105,0));
返回0;
}

我这里有一个肮脏的方式,但它能给你想要的

  #include <windows.h>
  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  SetConsoleTextAttribute(hConsole,30);
  system("CLS");
#包括
hConsole=GetStdHandle(标准输出句柄);
SetConsoleTextAttribute(hConsole,30);
系统(“CLS”);

听起来像是一个,但是在C++中……没有在快速Google中找到任何东西。也许只是设置文本背景为红色,然后打印一整排字符,大部分是空白,里面有文本,也有红色背景。我认为这可以作为一种解决办法。这在Windows命令语言中是微不足道的:<代码>颜色4F,以及那个。阿尔夫:这意味着这可以起作用:<代码>系统(“CMD/C”颜色4F\)“<代码> > @ LooMead工作得很好。我知道它通常不推荐,不可移植,等等,但我认为这是我目前唯一的选择。除非我能想出如何在C++中做同样的事情(我确信这是可能的)。。非常接近,但已包含字符的单元格仍保留以前的背景色。我想我的Win32/控制台有点生锈了。你可以试试吗?这可能就是你想要的。+1简单但有效,即使使用
system
调用速度较慢。我最终选择了此选项。我本希望使用API函数,但这s非常简单,效果非常好。不起作用!它会更改文本颜色和文本背景颜色而不是整个控制台的颜色此答案与上面的@MaGetzUb答案有什么不同?它们都显示了如何使用GetConsoleScreenBufferInfoEx和SetConsoleScreenBufferInfoEx,但您的答案似乎不必要地更复杂。实际上,我的代码非常复杂功能不同,尽管看起来很相似。@MaGetzUb的代码会更改与最后一行具有相同属性的所有字符的前景色和背景色。如果在此之前未使用SetConsoleTextAttribute,则整个背景都会更改。但是,请在之前添加:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x96);printf(“某个以前的控制台输出”;
。但它失败了。我的示例只更改一种颜色,即背景颜色,而不管以前打印的是什么。另外,您还可以将其设置为自定义RGB。出于某种原因,此解决方案导致我的控制台窗口每次颜色更改都会缩小
 #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.
/*