哪个 C++ 函数更改文本或背景颜色 (MS Visual studio)?例如cout<<"This text";
如何使“此文本”变为红色。
最佳答案
您可以使用 Win32 更改控制台应用程序的颜色,以下是如何更改颜色的示例:
#include "stdafx.h"
#include <Windows.h>
#include <iostream>
using namespace std;
int main(void)
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
if (hStdout == INVALID_HANDLE_VALUE)
{
cout << "Error while getting input handle" << endl;
return EXIT_FAILURE;
}
//sets the color to intense red on blue background
SetConsoleTextAttribute(hStdout, FOREGROUND_RED | BACKGROUND_BLUE | FOREGROUND_INTENSITY);
cout << "This is intense red text on blue background" << endl;
//reverting back to the normal color
SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
return EXIT_SUCCESS;
}
查看 SetConsoleTextAttribute
的 MSDN 文档功能和Console Screen Buffers获取更多信息。
有关使用 Win32 的控制台应用程序的更完整示例可用 here .
关于c++ - 如何在 Windows 控制台应用程序中更改文本或背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8285825/