【问题标题】:Color console output with C++ in Windows在 Windows 中使用 C++ 进行颜色控制台输出
【发布时间】:2012-03-04 22:59:45
【问题描述】:

有没有办法将彩色文本输出到控制台? 我使用的是 Visual Studio 2010,只需要代码就可以在 Windows 中工作。

除了 windows COLOR 命令外,我没有成功找到任何东西,但这改变了整个屏幕的颜色,我正在寻找只会改变我希望输出的部分的东西。 我已经看到它是在托管 C++ 中完成的

例如,

{color red}
cout << "Hello ";
{color blue}
cout << "world\n";

会产生红色和蓝色的“Hello world”。

【问题讨论】:

标签: c++ visual-studio-2010 windows-console


【解决方案1】:

我从here获取此代码:

// 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;
}

【讨论】:

  • 是的,刚刚发现。来这里结束我的问题。不过谢谢!
  • 在我能够做到之前必须等待一点:P
【解决方案2】:

在 Windows 中为 C++ 输出着色是通过 SetConsoleTextAttribute 完成的,其中控制台的 HANDLE 与属性一起传入。但是,调用 SetConsoleTextAttribute 很麻烦。幸运的是,互联网和 github 上有很多可以提供帮助的小型库,您应该选择一个具有您喜欢的 API 的库。如果你想用 operatorhttps://github.com/ikalnitsky/termcolor。 api 如下所示:

using namespace termcolor;
std::cout << grey    << "grey message"    << reset << std::endl;
std::cout << red     << "red message"     << reset << std::endl;

如果不得不重置颜色让你失望,试试我的图书馆。它也只有标题,仅限 Windows,它让您可以轻松地为 printf 语句着色:https://github.com/jrebacz/colorwin。 api 如下所示:

using namepsace wincolor;
std::cout << color(gray) << "grey message\n";
std::cout << color(red) << "red message\n";

std::cout << "normal color\n";
{
    withcolor scoped(red);
    std::cout << "|red\n";
    std::cout << "|red again\n";
}
std::cout << "normal color\n";
withcolor(cyan).printf("A cyan printf of %d\n", 1234);

【讨论】:

    【解决方案3】:

    这是我们的内部解决方案:

    inline void setcolor(int textcol, int backcol)
    {
        if ((textcol % 16) == (backcol % 16))textcol++;
        textcol %= 16; backcol %= 16;
        unsigned short wAttributes = ((unsigned)backcol << 4) | (unsigned)textcol;
        HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        SetConsoleTextAttribute(hStdOut, wAttributes);
    }
    

    以下是可供选择的颜色示例:

    #define LOG_COLOR_WHITE 7
    #define COLOR_GREEN 10
    #define COLOR_YELLOW 14 
    #define COLOR_MAGENTA 13
    

    【讨论】:

      【解决方案4】:

      可以使用system("")命令,就是这样使用的:

      cout<<"lol";
      system("color 1") // the colours are from 1 to 15. 
      cout<<"Coloured text! yay";
      

      【讨论】:

      • 这个答案是错误的:color [fb]:设置前景f和背景g 颜色。 from microsoft.com
      • 颜色也是十六进制格式,0-f。如果输入 15,您将设置前景色和背景色!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多