哪个 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;
}
查看 MSDN 文档了解SetConsoleTextAttribute http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047.aspx功能和控制台屏幕缓冲区 http://msdn.microsoft.com/en-us/library/windows/desktop/ms682088%28v=vs.85%29.aspx#_win32_character_attributes了解更多信息。
提供了有关使用 Win32 的控制台应用程序的更完整示例here http://msdn.microsoft.com/en-us/library/windows/desktop/ms686974%28v=vs.85%29.aspx.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)