在C中使用printf的彩色文本

时间:2012-01-06 23:42:06

标签: c windows colors printf

我想知道如何在控制台中打印彩色文字?我使用eclipse win64 os。它与编译器有关吗?任何人都可以在C中给出一个简单的例子,只有一个红色或任何颜色的hello世界文本吗?

6 个答案:

答案 0 :(得分:18)

我知道这在C ++中非常容易实现,但我发现这可以在C中查看:

#include <stdio.h>
#include <windows.h>   // WinApi header

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++)
  {
    SetConsoleTextAttribute(hConsole, k);
    printf("%3d  %s\n", k, "I want to be nice today!");
  }

  getchar();  // wait
  return 0;
}

所有评论都可以帮助您找到完成代码的方式 - 希望它有所帮助!

答案 1 :(得分:5)

如果要在Windows控制台中打印彩色文本,则必须使用Windows API。 Windows中不再存在ANSI.sys支持。

在Linux中,您仍然可以使用ANSI转义序列来着色文本。

答案 2 :(得分:3)

您也可以通过以下修改在 Windows 上使用 ANSI 转义序列:

reg add HKEY_CURRENT_USER\Console /v VirtualTerminalLevel /t REG_DWORD /d 0x00000001 /f

示例:

int main() 
{
   printf("\033[33mThis is yellow\033[0m");
   return 0;
}

来源: https://www.codeproject.com/Tips/5255355/How-to-Put-Color-on-Windows-Console

答案 3 :(得分:2)

如果您仅限于使用printf(),则需要了解您正在撰写的终端。它有可能是一个ANSI风格的终端,所以它可以完成。 Unix curses(Linux ncurses)库以与终端无关的方式处理此类信息。基本上,您需要定义或制作控制字符串以将终端转换为red模式,然后再将其重置(但在将其更改为写入红色文本之前,您如何知道它处于什么状态?)。提到的图书馆跟踪州的信息,以及许多其他细节。

但是,如果你把字符串组织起来,那么像这样的代码就可以了(或多或少):

static const char to_red[] = "\033...";
static const char to_black[] = "\033...";

printf("%s%s%s\n", to_red, "hello world", to_black);

困难的部分是确定常量字符串中的内容(实际上不需要是常数)。

所有这些意味着可能有一个特定于Windows的界面可用于完成这项工作,但这并不涉及printf()来控制颜色;您调用Windows API来设置颜色,然后使用printf()进行编写,然后再次调用API以恢复颜色。可能有一个查询功能,允许您在更改之前找到当前设置。

答案 4 :(得分:0)

Java中的控制台使用stdout,这是您运行的任何操作系统。对于Windows,您需要访问Console API才能更改颜色。对于Linux或Mac,控制台可能支持ANSI escape sequences,它可以通过stdout更改控制台颜色。

答案 5 :(得分:0)

这是为您提供的另一个示例。它是用C ++语言编写的,但是我敢肯定您可以处理。但是我在python的这个示例中也有完全相同的代码。这是我写的一个小演示,最终用颜色绘制了一些线条。

enter image description here

无论如何,演示系列位于:

https://github.com/goblinhack/c-plus-plus-python-tutorial

有关以下示例的完整信息,请访问:

https://github.com/goblinhack/c-plus-plus-python-tutorial/blob/master/lesson1/lesson1.cpp

上面图片的C ++代码使用的是我在lesson1.cpp中定义的Ansi颜色类。但是,一旦使用它,它的使用就非常简单。 hth。

int main (int argc, char *argv[])
{
    Ansi ansi;

    std::cout << ansi.get_code(ansi.FOREGROUND_RED);
    std::cout << "hello ";

    std::cout << ansi.get_code(ansi.FOREGROUND_GREEN);
    std::cout << "beautiful";
    std::cout << ansi.get_code(ansi.RESET);

    std::cout << ansi.get_code(ansi.FOREGROUND_CYAN);
    std::cout << " colorful";
    std::cout << ansi.get_code(ansi.RESET);

    std::cout << ansi.get_code(ansi.FOREGROUND_BLUE);
    std::cout << " world";
    std::cout << ansi.get_code(ansi.RESET);
    std::cout << std::endl;

    return (0);
}

在python中具有类似功能

 def lesson1():
        """ hello beautiful world """
        ansi = Ansi()

        for bg_col in range(ansi.Code.BACKGROUND_BLACK,
                            ansi.Code.BACKGROUND_WHITE):
            for fg_col in range(ansi.Code.FOREGROUND_BLACK,
                                ansi.Code.FOREGROUND_WHITE):
                sys.stdout.write("{0: <20} {1: <20}".format(\
                                 ansi.get_code_name(bg_col),
                                 ansi.get_code_name(fg_col)))
                sys.stdout.write(ansi.get_bgfg_code(bg_col, fg_col))
                sys.stdout.write("colorful")
                sys.stdout.write(ansi.get_code(ansi.Code.RESET))
                print()

        sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_RED))
        sys.stdout.write("hello")

        sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_GREEN))
        sys.stdout.write(" beautiful")

        sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_CYAN))
        sys.stdout.write(" colorful")

        sys.stdout.write(ansi.get_code(ansi.Code.FOREGROUND_BLUE))
        sys.stdout.write(" world")

        sys.stdout.write(ansi.get_code(ansi.Code.RESET))
        print("from Python")

    lesson1()
相关问题