【问题标题】:how to get background color back to previous color after use of std handle使用std句柄后如何将背景颜色恢复为以前的颜色
【发布时间】:2014-10-22 22:08:21
【问题描述】:

我正在使用 Visual Studio for C++,我们正在编写我们的第一个代码,但是我遇到了一个“简单”的问题。

在代码中,我将每个部分本身用作一个函数,因此对于显示“按 Enter”的输出屏幕,它正在调用一个函数以在屏幕上显示它。但是,在告别时,我将系统颜色更改为背景为白色,文本为黑色,但仍然需要显示“Hit enter”功能。确实如此,但由于它使用自己的颜色,所以现在在 cout 中的“\t”处有一行颜色。

我如何在它不会这样做的地方得到它?

    #include <iostream>             //Necessary for input/output
    #include <string>               //Necessary for constants
    #include <Windows.h>            //Necessary for colored text

    using namespace std;

呃...我以前从未这样做过...但我将单独发布这些部分。

    system("cls");

system("color F0");

cout << "\n\n\n\n\n\n\n\t\tIt was a pleasure spending time with you, "
    "User"
    "!\n\n\n";

cout << "\t\t\t\350";
for (int i = 0; i < 31; i++){ cout << "\360"; }
cout << "\350\n";
cout << "\t\t\t\272";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); //light grey
cout << "     \311\315\273\332\304\277\332\304\277\332\302\277\332\277 \302 \302\332\304\277     ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240); //Black text white bg
cout << "\272\n"
    "\t\t\t\272";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); //light grey
cout << "     \272 \313\263 \263\263 \263 \263\263\303\301\277\300\302\331\303\264      ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240); //Black text white bg
cout << "\272\n"
    "\t\t\t\272";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15); //light grey
cout << "     \310\315\274\300\304\331\300\304\331\304\301\331\300\304\331 \301 \300\304\331     ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240); //Black text white bg
cout << "\272\n";
cout << "\t\t\t\350";
for (int i = 0; i < 31; i++){ cout << "\360"; }
cout << "\350\n";

对于 Hit 输入功能

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); //light grey
cout << "\n\n\n\t\t\t\332";
for (int i = 0; i < 28; i++){ cout << "\304"; }
cout << "\277\n\t\t\t\263 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // Red
cout << "\3 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 71); //light grey text with red BG
cout << "Please press the Enter";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // Red
cout << " \3 ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); //light grey
cout << "\263\n\t\t\t\324";
for (int i = 0; i < 28; i++){ cout << "\315"; }
cout << "\276";

“浅灰色”区域也为选项卡着色......我该怎么办。对不起,如果这一切都很糟糕。这方面的新手。

【问题讨论】:

    标签: c++ colors std handle


    【解决方案1】:

    我将把我的 Pascal 代码移植到 C++ 中。我已经对其进行了测试。解决方案是在通过attributes设置颜色之前使用GetConsoleScreenBufferInfo..然后在之后立即恢复attributes..

    #include <windows.h>
    #include <stdio.h>
    
    void SetConsoleColour(WORD* Attributes, DWORD Colour)
    {
        CONSOLE_SCREEN_BUFFER_INFO Info;
        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfo(hStdout, &Info);
        *Attributes = Info.wAttributes;
        SetConsoleTextAttribute(hStdout, Colour);
    }
    
    void ResetConsoleColour(WORD Attributes)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), Attributes);
    }
    
    int main()
    {
        WORD Attributes = 0;
        SetConsoleColour(&Attributes, FOREGROUND_INTENSITY | FOREGROUND_RED);
        printf("Foreground change..\n");
        ResetConsoleColour(Attributes);
    
        printf("Normal attributes..\n");
    
        SetConsoleColour(&Attributes, BACKGROUND_INTENSITY | BACKGROUND_RED);
        printf("Background change..\n");
        ResetConsoleColour(Attributes);
        printf("Normal attributes..\n");
    
    
        SetConsoleColour(&Attributes, FOREGROUND_INTENSITY | FOREGROUND_RED);
        printf("Mixture");
        ResetConsoleColour(Attributes);
        printf(" of ");
        SetConsoleColour(&Attributes, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
        printf("both..\n");
        ResetConsoleColour(Attributes);
    
    
        SetConsoleColour(&Attributes, FOREGROUND_INTENSITY | FOREGROUND_RED);
        printf("Mixture");
        ResetConsoleColour(Attributes);
        printf(" of ");
        SetConsoleColour(&Attributes, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
        printf("all ");
        ResetConsoleColour(Attributes);
        SetConsoleColour(&Attributes, BACKGROUND_INTENSITY | BACKGROUND_BLUE);
        printf("three");
        ResetConsoleColour(Attributes);
        printf(" ");
        SetConsoleColour(&Attributes, BACKGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_BLUE | FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
        printf("in a single line\n");
        ResetConsoleColour(Attributes);
    }
    

    您也可以通过以相同方式保存其属性来为“先前”颜色执行此操作。您可以编写自己的函数来更改颜色、打印然后为您重置。您可以为彩色 INPUT 执行此操作好吧..取决于你如何使用这些..

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 2011-11-11
      • 2013-03-19
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      相关资源
      最近更新 更多