本文介绍了在 Windows 中使用 C++ 的颜色控制台输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!
问题描述
有没有办法将彩色文本输出到控制台?我使用的是 Visual Studio 2010,只需要代码即可在 Windows 中工作.
Is there a way to output colored text to the console? I am using Visual Studio 2010, and only need the code to work in Windows.
除了 windows COLOR 命令之外,我一直没有找到任何东西,但这改变了整个屏幕的颜色,我正在寻找只会改变我希望输出的部分的东西.我已经看到它在托管 C++ 中完成
I have been unsuccessful in finding anything except the windows COLOR command, but that changed the color for the entire screen, and I am looking for something that will change only the part I wish to output. I've seen it done in Managed C++
例如,
{color red}
cout << "Hello ";
{color blue}
cout << "world
";
将产生红色和蓝色的Hello world".
would yield "Hello world" in red and blue.
推荐答案
我从 此处:
// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// a Dev-C++ tested console application by vegaseat 07nov2004
#include <iostream>
#include <windows.h> // WinApi header
using namespace std; // std::cout, std::cin
int main()
{
HANDLE hConsole;
int k;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// you can loop k higher to see more color choices
for(k = 1; k < 255; k++)
{
// pick the colorattribute k you want
SetConsoleTextAttribute(hConsole, k);
cout << k << " I want to be nice today!" << endl;
}
cin.get(); // wait
return 0;
}
这篇关于在 Windows 中使用 C++ 的颜色控制台输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!