Bec*_*ard 1 c++ colors std handle
我正在使用Visual Studio for C++,我们正在编写第一个代码,但是我遇到了一个"简单"的问题.
在代码中,我将每个部分作为一个函数本身使用,因此对于输出屏幕,它显示"命中输入",它调用一个函数在屏幕上显示.然而,在告别中,我更改了系统颜色,使背景为白色,文本为黑色,但仍需要显示"命中输入"功能.它确实如此,但由于它使用了它自己的颜色,现在有一条颜色,其中"\ t"在cout中.
如何在不会这样做的地方得到它?
#include <iostream> //Necessary for input/output
#include <string> //Necessary for constants
#include <Windows.h> //Necessary for colored text
using namespace std;
Run Code Online (Sandbox Code Playgroud)
呃......我以前从未这样做过......但我会单独发布这些部分.
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";
Run Code Online (Sandbox Code Playgroud)
并为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";
Run Code Online (Sandbox Code Playgroud)
"浅灰色"区域也为标签着色....我该怎么做.对不起,如果这太可怕了.新的.
我要将我的Pascal代码移植到C++ ..它的工作原理我已经测试过了.解决方案是GetConsoleScreenBufferInfo在设置颜色之前使用attributes..然后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);
}
Run Code Online (Sandbox Code Playgroud)
您也可以通过以相同方式保存其属性来为"之前"颜色执行此操作.您可以编写自己的功能来更改颜色,打印然后重置为您.您也可以为彩色INPUT执行此操作.由您决定如何使用这些..
| 归档时间: |
|
| 查看次数: |
8223 次 |
| 最近记录: |