微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Windows中terminal应用程序中的C颜色文本

我知道“textcolor();” 是为C + +,我已经看到了unix的方法…但也有窗户的方式吗?

#include <stdio.h> int main() { printf("ntest - C programming text color!"); printf("n--------------------------------"); printf("nntt-BREAK-nn"); textcolor(15); printf("WHITEn"); textcolor(0); printf("BLACKn"); textcolor(4); printf("Redn"); textcolor(1); printf("BLUEn"); textcolor(2); printf("GREENn"); textcolor(5); printf("magentan"); textcolor(14); printf("YELLOWn"); textcolor(3); printf("CYANn"); textcolor(7); printf("LIGHT GRAYn"); }

我在网上找不到任何东西…让我们希望从堆栈溢出的好人可以帮助:D

请C,而不是C ++

从Windowsbatch file中replace文本文件中的字符

在terminalinput空字符

如何在Linux中的多行查找和replace文本?

批处理脚本在文本文件中打印searchstring的前一行和下一行

C ++在窗口上输出文本

Windows版本的wcswidth_l

Linux – 从第二个标签获取文本

什么从RStudio中运行.bat文件

是否有任何计划的IE9支持webKit像文字阴影?

在文本文件的最后一行之前添加文本

既然你想要一个C和Windows特定的解决方案,我建议在Win32 API中使用SetConsoleTextAttribute()函数。 您需要获取控制台的句柄,然后将其传递给相应的属性

一个简单的例子:

/* Change console text color,then restore it back to normal. */ #include <stdio.h> #include <windows.h> int main() { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO consoleInfo; WORD saved_attributes; /* Save current attributes */ GetConsoleScreenBufferInfo(hConsole,&consoleInfo); saved_attributes = consoleInfo.wAttributes; SetConsoleTextAttribute(hConsole,FOREGROUND_BLUE); printf("This is some nice COLORFUL text,isn't it?"); /* Restore original attributes */ SetConsoleTextAttribute(hConsole,saved_attributes); printf("Back to normal"); return 0; }

有关可用属性的更多信息,请看这里 。

希望这可以帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐