gpt4 book ai didi

c++ - 在 Windows 控制台中更改背景和文本颜色,c++

转载 作者:太空宇宙 更新时间:2023-11-04 13:47:09 25 4
gpt4 key购买 nike

我使用以下代码更改控制台的颜色。但它也会突出显示文本。我不希望我的文本被突出显示并且想要白色背景。

    using namespace std;
#include<conio.h>
#include<stdlib.h>
#include <iostream>
#include <windows.h>
using namespace std;

//returns the current attributes
WORD GetConsoleTextAttribute (HANDLE hCon)
{
CONSOLE_SCREEN_BUFFER_INFO con_info;
GetConsoleScreenBufferInfo(hCon, &con_info);

return con_info.wAttributes;
}

int main (void)
{
system("color F1");

HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
const int saved_colors = GetConsoleTextAttribute(hConsole);



SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "This text should be blue" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "This text should be green" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
getch();
return 0;
}

最佳答案

自己制作白色背景

WORD whiteBack = BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY;
SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY | whiteBack);
cout << "This text should be blue" << endl;
SetConsoleTextAttribute(hConsole, saved_colors);
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY | whiteBack);
cout << "This text should be green" << endl;

关于c++ - 在 Windows 控制台中更改背景和文本颜色,c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25402017/

25 4 0
文章推荐: jquery - 自定义数据属性在