windows终端应用程序中的C颜色文本 我知道“TrimeCube()”,是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"); } #包括 int main() { printf(“\ntest-C编程文本颜色!”); printf(“\n----------------------------------------------------”; printf(“\n\n\t\t-BREAK-\n\n”); textcolor(15); printf(“白色”); textcolor(0); printf(“黑色”); textcolor(4); printf(“红色”); textcolor(1); printf(“蓝色”); textcolor(2); printf(“绿色”); textcolor(5); printf(“洋红色”); textcolor(14); printf(“黄色”); textcolor(3); printf(“青色”); textcolor(7); printf(“浅灰色”); }
我在网上找不到任何东西。。。 让我们希望来自stack overflow的优秀人士能够提供帮助:D 请用C,不要用C++给你:windows终端应用程序中的C颜色文本 我知道“TrimeCube()”,是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"); } #包括 int main() { printf(“\ntest-C编程文本颜色!”); printf(“\n----------------------------------------------------”; printf(“\n\n\t\t-BREAK-\n\n”); textcolor(15); printf(“白色”); textcolor(0); printf(“黑色”); textcolor(4); printf(“红色”); textcolor(1); printf(“蓝色”); textcolor(2); printf(“绿色”); textcolor(5); printf(“洋红色”); textcolor(14); printf(“黄色”); textcolor(3); printf(“青色”); textcolor(7); printf(“浅灰色”); },c,windows,text,terminal,textcolor,C,Windows,Text,Terminal,Textcolor,我在网上找不到任何东西。。。 让我们希望来自stack overflow的优秀人士能够提供帮助:D 请用C,不要用C++给你: 您可以在这里看到它的一种用法,因此:由于您需要C和Windows特定的解决方案,我建议您在Win32 API中使用SetConsoleTextAttribute()函数。您需要抓取控制台的句柄,然后使用适当的属性传递它 举个简单的例子: /* Change console text color, then restore it back to normal. */ #i
您可以在这里看到它的一种用法,因此:由于您需要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;
}
/*更改控制台文本颜色,然后将其恢复正常*/
#包括
#包括
int main(){
HANDLE hConsole=GetStdHandle(标准输出句柄);
控制台\屏幕\缓冲区\信息控制台信息;
字保存属性;
/*保存当前属性*/
GetConsoleScreenBufferInfo(hConsole和consoleInfo);
保存的属性=consoleInfo.wAttributes;
SetConsoleExtAttribute(hConsole,前景为蓝色);
printf(“这是一些漂亮的彩色文本,不是吗?”);
/*恢复原始属性*/
SetConsoleExtAttribute(hConsole,保存的_属性);
printf(“恢复正常”);
返回0;
}
有关可用属性的更多信息,请参见
希望这有帮助!:) 我添加了一个简单的脚本来实现这一点,需要考虑以下几点:
#包括
#包括
#包括
void InitConsole()
{
单词wColor=(背景为绿色,前景为蓝色);
HANDLE handleConsole=GetStdHandle(STD_OUTPUT_HANDLE);/*当前输出缓冲区的句柄*/
COORD COORD={0,0};
德沃德计数;
控制台屏幕缓冲区信息控制台缓冲区;
SetConsoleTextAttribute(handleConsole,wColor);
if(GetConsoleScreenBufferInfo(handleConsole和consoleBuffer))
填写ConsoleOutput属性(handleConsole、consoleBuffer.wAttributes、consoleBuffer.dwSize.X*consoleBuffer.dwSize.Y、coord和count);
返回;
}
int main()
{
InitConsole();
SetConsoleTitle(“迷你桌面应用程序”);
而(1){
printf(“按预期工作\n”);
printf(“按任意键退出:)\n”);
getch();
打破
}
返回0;
}
使用定义的参数
#包括
#包括
#包括
void InitConsole(int ForgC,int BackC)
{
单词wColor=((BackC&0x0F)
非常感谢,它工作得很好,只是想知道如何将其设置回默认的浅灰色?非常感谢!@JoeDF要做到这一点,您需要使用GetConsoleScreenBufferInfo()
读入原始属性,将它们存储在变量中,然后在完成后还原。我已更新了答案以说明如何执行此操作。:)“JoeDF不是问题。如果你认为你的问题已经得到解决,请自由回答……”