如何在 C++ 控制台应用程序中设置整行背景颜色

How do I set the whole rows background color in a C++ console app

我该如何按照主题行中的说明进行操作?

下面是我的控制台应用程序的屏幕截图以及一些代码。

switch (indicator) {
    case GreenFlag:
        indicator = GreenFlag;
        system("CLS");
        SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_GREEN);
        cout << "IF YOUR CAR ISN'T LEAKING OIL AND YOU DON'T MAKE A NOISE YOU'RE WELCOME TO PARK HERE!" << endl << endl;
        break;
        ...
    default:
        system("CLS");
        SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
        cout << "INCORRECT OPTION, PLEASE TRY AGAIN!" << endl << endl;
        break;
}
SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), 7);

这就是我最后想到的!

我认为它很扎实,但它仍然需要相当多的工作。

@IInspectable 我仍在寻找清除屏幕的最佳方法,以确保它能在未来得到证明。

string message = "";
// Indicator mood;
// mood = (Indicator)Random.Range(0, System.Enum.GetValues(typeof(Indicator)).Length);)

switch (indicator) {
    case GreenFlag:
        indicator = GreenFlag;
        // TODO: Need to replace system("CLS") with a more portable option, take a look at the following link(s):
        // https://www.cplusplus.com/articles/4z18T05o/ and https://www.cplusplus.com/forum/beginner/3304/ for more information!
        system("CLS");
        SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_GREEN);
        message = "IF YOUR CAR ISN'T LEAKING OIL AND YOU DON'T MAKE A NOISE YOU'RE WELCOME TO PARK HERE!";
        cout << message.append(120 - message.length(), ' ') << endl << endl;
        break;
...
default:
        system("CLS");
        SetConsoleTextAttribute(GetStdHandle (STD_OUTPUT_HANDLE), BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
        message = "INCORRECT OPTION, PLEASE TRY AGAIN!";
        cout << message.append(120 - message.length(), ' ') << endl << endl;
        break;
}

再次感谢@Jeffrey 和@IInspectable 的想法,感谢中肯的建议 ;-)。