gpt4 book ai didi

c++ - 使用标准句柄后如何将背景颜色恢复为以前的颜色

转载 作者:行者123 更新时间:2023-11-30 00:50:16 35 4
gpt4 key购买 nike

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

在代码中,我将每个部分本身用作一个函数,因此对于显示“按回车键”的输出屏幕,它正在调用一个函数来在屏幕上显示它。不过在告别时,我把系统颜色改成了背景白,文字黑,但是“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 enter函数

        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";

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

最佳答案

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

enter image description here

#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);
}

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

关于c++ - 使用标准句柄后如何将背景颜色恢复为以前的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25559077/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com