我想编写一个Console类,可以将彩色文本输出到控制台。
所以我可以做类似的事情(基本上是printf的包装器):
Console::Print( "This is a non-coloured message\n" );
Console::Warning( "This is a YELLOW warning message\n" );
Console::Error( "This is a RED error message\n" );
如何将不同颜色的文本打印到Windows控制台?
答案 0 :(得分:9)
结帐this guide。我会做一个自定义操纵器,所以我可以做类似的事情:
std::cout << "standard text" << setcolour(red) << "red text" << std::endl;
Here是关于如何实施自己的操纵器的小指南。
快速代码示例:
#include <iostream>
#include <windows.h>
#include <iomanip>
using namespace std;
enum colour { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
struct setcolour
{
colour _c;
HANDLE _console_handle;
setcolour(colour c, HANDLE console_handle)
: _c(c), _console_handle(0)
{
_console_handle = console_handle;
}
};
// We could use a template here, making it more generic. Wide streams won't
// work with this version.
basic_ostream<char> &operator<<(basic_ostream<char> &s, const setcolour &ref)
{
SetConsoleTextAttribute(ref._console_handle, ref._c);
return s;
}
int main(int argc, char *argv[])
{
HANDLE chandle = GetStdHandle(STD_OUTPUT_HANDLE);
cout << "standard text" << setcolour(RED, chandle) << " red text" << endl;
cin.get();
}
答案 1 :(得分:1)
我搜索了“c ++ console write coloured text”,并在大约4或5时提出this page。因为该网站有一个副本&amp;粘贴部分我以为我会在这里发布(关于链接腐烂的另一个问题也提示了这一点):
#include <stdlib.h>
#include <windows.h>
#include <iostream>
using namespace std;
enum Color { DBLUE=1,GREEN,GREY,DRED,DPURP,BROWN,LGREY,DGREY,BLUE,LIMEG,TEAL,
RED,PURPLE,YELLOW,WHITE,B_B };
/* These are the first 16 colors anyways. You test the other hundreds yourself.
After 15 they are all combos of different color text/backgrounds. */
bool quit;
void col(unsigned short color)
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hcon,color);
}
istream &operator>> ( istream &in, Color &c )
{
int tint;
cin >> tint;
if (tint==-1) quit=true;
c=(Color)tint;
}
int main()
{
do {
col(7); // Defaults color for each round.
cout << "Enter a color code, or -1 to quit... ";
Color y;
cin >> y; // Notice that >> is defined above for Color types.
col(y); // Sets output color to y.
if (!quit) cout << "Color: " << (int)y << endl;
} while (!quit);
return 0;
}
对于C#,有this page
答案 2 :(得分:0)
#include <windows.h>
#include <iostream.h>
using namespace std;
int main()
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOut,
BACKGROUND_RED);
cout << "Red " << flush;
SetConsoleTextAttribute(hOut,
BACKGROUND_RED |
BACKGROUND_INTENSITY);
cout << "Red " << endl;
SetConsoleTextAttribute(hOut,
BACKGROUND_GREEN);
cout << "Green " << flush;
SetConsoleTextAttribute(hOut,
BACKGROUND_GREEN |
BACKGROUND_INTENSITY);
cout << "Green " << endl;
SetConsoleTextAttribute(hOut,
BACKGROUND_BLUE);
cout << "Blue " << flush;
SetConsoleTextAttribute(hOut,
BACKGROUND_BLUE |
BACKGROUND_INTENSITY);
cout << "Blue " << endl;
SetConsoleTextAttribute(hOut,
BACKGROUND_RED |
BACKGROUND_GREEN);
cout << "Yellow " << flush;
SetConsoleTextAttribute(hOut,
BACKGROUND_RED |
BACKGROUND_GREEN |
BACKGROUND_INTENSITY);
cout << "Yellow " << endl;
SetConsoleTextAttribute(hOut,
BACKGROUND_GREEN |
BACKGROUND_BLUE);
cout << "Cyan " << flush;
SetConsoleTextAttribute(hOut,
BACKGROUND_GREEN |
BACKGROUND_BLUE |
BACKGROUND_INTENSITY);
cout << "Cyan " << endl;
SetConsoleTextAttribute(hOut,
BACKGROUND_BLUE |
BACKGROUND_RED);
cout << "Magenta " << flush;
SetConsoleTextAttribute(hOut,
BACKGROUND_BLUE |
BACKGROUND_RED |
BACKGROUND_INTENSITY);
cout << "Magenta " << endl;
SetConsoleTextAttribute(hOut,
BACKGROUND_RED |
BACKGROUND_GREEN |
BACKGROUND_BLUE);
cout << "White " << flush;
SetConsoleTextAttribute(hOut,
BACKGROUND_RED |
BACKGROUND_GREEN |
BACKGROUND_BLUE |
BACKGROUND_INTENSITY);
cout << "White " << endl;
return 0;
}
答案 3 :(得分:0)
使用这些功能
enum c_color{BLACK=30,RED=31,GREEN=32,YELLOW=33,BLUE=34,MAGENTA=35,CYAN=36,WHITE=37};
enum c_decoration{NORMAL=0,BOLD=1,FAINT=2,ITALIC=3,UNDERLINE=4,RIVERCED=26,FRAMED=51};
void pr(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m";
}
void prl(const string str,c_color color,c_decoration decoration=c_decoration::NORMAL){
cout<<"\033["<<decoration<<";"<<color<<"m"<<str<<"\033[0m"<<endl;
}