C ++颜色控制台文本

时间:2013-12-16 10:04:26

标签: c++

我到处搜寻,无法相信找到它有多难。 我有以下代码:

cout << "Hello";
cout << "Bye";

我希望第一行是绿色,第二行是红色。 我找到了:

system(" color 2");

然而,这会使控制台中的所有文字绿色

我只需要它在Windows 7上运行

5 个答案:

答案 0 :(得分:4)

不幸的是。没有原生的可移植方式来执行此操作,因为bash / linux将使用ANSI转义码(与较旧的DOS一样),但Windows XP或更高版本将使用Console API(文本颜色等)调用。

您可能会获得第三方库,可以为两者提供单一的API(ncurses / p-curses)。

看看here

答案 1 :(得分:2)

对我有用的方法是使用ANSI escape codes,但我不使用Windows(它可能仍然可以使用):

#include <iostream>

int main()
{
    std::cout << "\x1b[32;1mHello\x1b[0m\n";
    std::cout << "\x1b[31;1mBye\x1b[0m\n";
}

答案 2 :(得分:2)

如果您使用的是Windows,则可以使用SetConsoleTextAttribute将文本着色为任何颜色:

void change_color(const int color_flags)
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute( hConsole, color_flags );
}
template <class T>
void print_colored(const int color_flags,const T & arg)
{
    change_color(color_flags);
    cout << arg;
    change_color(FOREGROUND_RED   | FOREGROUND_GREEN | FOREGROUND_BLUE);//back to normal
}

print_colored(FOREGROUND_RED,"stuff");//this will be red

您可以在着色标记here

上找到更多信息

答案 3 :(得分:0)

在具有ANSI功能的终端上的c ++中,可以编写自己的ansi流操纵器,如std :: endl,但使用时可通过更可读的方式处理ansi转义代码。

执行此操作的代码对于基本的原始实现应如下所示:

namespace ansi {
  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & reset( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "\033[0m";
  }

  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & foreground_black( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "\033[30m";
  }

  template < class CharT, class Traits >
  constexpr
  std::basic_ostream< CharT, Traits > & foreground_red( std::basic_ostream< CharT, Traits > &os )
  {
     return os << "\033[31m";
  }
  ...
 } // ansi

它可以在这样的代码中使用:

std::cout << ansi::foreground_red << "in red" << ansi::reset << std::endl;

答案 4 :(得分:0)

以下是在Windows和Linux上均可使用的解决方案:

#include <string>
#include <iostream>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <Windows.h>
#endif // _WIN32

using namespace std;

#define color_black      0
#define color_dark_blue  1
#define color_dark_green 2
#define color_light_blue 3
#define color_dark_red   4
#define color_magenta    5
#define color_orange     6
#define color_light_gray 7
#define color_gray       8
#define color_blue       9
#define color_green     10
#define color_cyan      11
#define color_red       12
#define color_pink      13
#define color_yellow    14
#define color_white     15

void print(const string s="") {
    cout << s << endl;
}
void print(const string s, const int textcolor, const int backgroundcolor) {
#if defined(_WIN32)
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    WORD default_colors = 0;
    if(GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) default_colors = csbi.wAttributes;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), backgroundcolor<<4|textcolor);
#elif defined(__linux__)
    string t, b;
    switch(textcolor) {
        case  0: t="30"; break; // color_black      0
        case  1: t="34"; break; // color_dark_blue  1
        case  2: t="32"; break; // color_dark_green 2
        case  3: t="36"; break; // color_light_blue 3
        case  4: t="31"; break; // color_dark_red   4
        case  5: t="35"; break; // color_magenta    5
        case  6: t="33"; break; // color_orange     6
        case  7: t="37"; break; // color_light_gray 7
        case  8: t="90"; break; // color_gray       8
        case  9: t="94"; break; // color_blue       9
        case 10: t="92"; break; // color_green     10
        case 11: t="96"; break; // color_cyan      11
        case 12: t="91"; break; // color_red       12
        case 13: t="95"; break; // color_pink      13
        case 14: t="93"; break; // color_yellow    14
        case 15: t="97"; break; // color_white     15
        default: t="97";
    }
    switch(backgroundcolor) {
        case  0: b= "40"; break; // color_black      0
        case  1: b= "44"; break; // color_dark_blue  1
        case  2: b= "42"; break; // color_dark_green 2
        case  3: b= "46"; break; // color_light_blue 3
        case  4: b= "41"; break; // color_dark_red   4
        case  5: b= "45"; break; // color_magenta    5
        case  6: b= "43"; break; // color_orange     6
        case  7: b= "47"; break; // color_light_gray 7
        case  8: b="100"; break; // color_gray       8
        case  9: b="104"; break; // color_blue       9
        case 10: b="102"; break; // color_green     10
        case 11: b="106"; break; // color_cyan      11
        case 12: b="101"; break; // color_red       12
        case 13: b="105"; break; // color_pink      13
        case 14: b="103"; break; // color_yellow    14
        case 15: b="107"; break; // color_white     15
        default: b= "40";
    }
    cout << "\033["+t+";"+b+"m";
#endif // Windows/Linux
    cout << s;
#if defined(_WIN32)
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), default_colors);
#elif defined(__linux__)
    cout << "\033[0m"; // reset color
#endif // Windows/Linux
}
void wait() {
    cin.get();
}

void print_color_test() {
    print(" ", color_black, color_magenta   );
    print(" ", color_black, color_blue      );
    print(" ", color_black, color_light_blue);
    print(" ", color_black, color_cyan      );
    print(" ", color_black, color_green     );
    print(" ", color_black, color_yellow    );
    print(" ", color_black, color_orange    );
    print(" ", color_black, color_red       );
    print();
    print(" ", color_black, color_black     );
    print(" ", color_black, color_gray      );
    print(" ", color_black, color_light_gray);
    print(" ", color_black, color_white     );
    print(" ", color_black, color_pink      );
    print(" ", color_black, color_dark_blue );
    print(" ", color_black, color_dark_green);
    print(" ", color_black, color_dark_red  );
    print();
    print("#", color_magenta   , color_black);
    print("#", color_blue      , color_black);
    print("#", color_light_blue, color_black);
    print("#", color_cyan      , color_black);
    print("#", color_green     , color_black);
    print("#", color_yellow    , color_black);
    print("#", color_orange    , color_black);
    print("#", color_red       , color_black);
    print();
    print("#", color_black     , color_black);
    print("#", color_gray      , color_black);
    print("#", color_light_gray, color_black);
    print("#", color_white     , color_black);
    print("#", color_pink      , color_black);
    print("#", color_dark_blue , color_black);
    print("#", color_dark_green, color_black);
    print("#", color_dark_red  , color_black);
    print();
}

int main() {
    print("test");
    print_color_test();
    print("test\n");

    print("Hello", color_green, color_black);
    print("Bye", color_red, color_black);

    wait();
    return 0;
}