Joe*_* DF 17 c windows terminal text textcolor
我知道"textcolor();" 是用于C++,我已经看到了unix的方法......但是对于Windows也有办法吗?
#include <stdio.h>
int main()
{
printf("\ntest - C programming text color!");
printf("\n--------------------------------");
printf("\n\n\t\t-BREAK-\n\n");
textcolor(15);
printf("WHITE\n");
textcolor(0);
printf("BLACK\n");
textcolor(4);
printf("RED\n");
textcolor(1);
printf("BLUE\n");
textcolor(2);
printf("GREEN\n");
textcolor(5);
printf("MAGENTA\n");
textcolor(14);
printf("YELLOW\n");
textcolor(3);
printf("CYAN\n");
textcolor(7);
printf("LIGHT GRAY\n");
}
Run Code Online (Sandbox Code Playgroud)
我在网上找不到任何东西......让我们希望堆栈溢出的好人可以提供帮助:D
C请,而不是C++
Mig*_*uel 35
由于您需要特定于C和Windows的解决方案,因此我建议您使用SetConsoleTextAttribute()Win32 API中的该功能.您需要获取控制台的句柄,然后使用适当的属性传递它.
举个简单的例子:
/* 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;
}
Run Code Online (Sandbox Code Playgroud)
有关可用属性的更多信息,请查看此处.
希望这可以帮助!:)