在 C 中使用 printf 的彩色文本 [英] colorful text using printf in C
本文介绍了在 C 中使用 printf 的彩色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在控制台中打印彩色文本?我使用 eclipse win64 操作系统.和编译器有关系吗?任何人都可以用 C 语言举一个简单的例子,只有一个红色的 hello world 文本或其他什么?
I was wondering how can I print colorful text in the console? I use eclipse win64 OS. Does it have to do with the compiler? Can anyone give a simple example in C with just a hello world text in red or whatever?
推荐答案
我知道这在 C++ 中非常容易做到,但我发现这个让你在 C 中查看:
I know that this is incredibly easy to do in C++, but I found this for you to look at in 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
", k, "I want to be nice today!");
}
getchar(); // wait
return 0;
}
所有评论都将帮助您找到代码的方式 - 希望对您有所帮助!
All of the comments will help you to find your way through the code - hope it helps!
这篇关于在 C 中使用 printf 的彩色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文