我知道“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");
}
我在网上找不到任何东西...... 希望来自 stackoverflow 的好心人能够提供帮助:D
请使用 C,而不是 C++
由于您需要 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;
}
有关可用属性的更多信息,请查看此处。
希望这有帮助! :)
我添加了一个简单的脚本来实现此目的,一些注意事项:
#include <stdio.h>
#include <windows.h>
#include <conio.h>
void InitConsole()
{
WORD wColor = (BACKGROUND_GREEN | FOREGROUND_BLUE);
HANDLE handleConsole = GetStdHandle(STD_OUTPUT_HANDLE); /* Handle to current output buffer*/
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO consoleBuffer;
SetConsoleTextAttribute(handleConsole, wColor);
if (GetConsoleScreenBufferInfo(handleConsole, &consoleBuffer))
FillConsoleOutputAttribute(handleConsole, consoleBuffer.wAttributes, consoleBuffer.dwSize.X * consoleBuffer.dwSize.Y, coord, &count);
return;
}
int main()
{
InitConsole();
SetConsoleTitle("Mini Desktop App");
while(1){
printf("Works as expected\n");
printf("Press any Key to exit :)\n");
getch();
break;
}
return 0;
}
#include <stdio.h>
#include <windows.h>
#include <conio.h>
void InitConsole(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);
HANDLE handleConsole = GetStdHandle(STD_OUTPUT_HANDLE); /* Handle to current output buffer*/
COORD coord = {0, 0};
DWORD count;
CONSOLE_SCREEN_BUFFER_INFO consoleBuffer;
SetConsoleTextAttribute(handleConsole, wColor);
if (GetConsoleScreenBufferInfo(handleConsole, &consoleBuffer))
FillConsoleOutputAttribute(handleConsole, consoleBuffer.wAttributes, consoleBuffer.dwSize.X * consoleBuffer.dwSize.Y, coord, &count);
return;
}
int main()
{
InitConsole(15, 1);
SetConsoleTitle("Mini Desktop App");
while(1){
printf("Works as expected\n");
printf("Press any Key to exit :)\n");
getch();
break;
}
return 0;
}
有关控制台屏幕缓冲区的信息CONSOLE_SCREEN_BUFFER_INFO结构。
要确定屏幕缓冲区的当前颜色属性,请调用 GetConsoleScreenBufferInfo 函数。
从屏幕缓冲区中的指定坐标开始,设置指定数量的字符单元格的字符属性 --> FillConsoleOutputAttribute 函数
两种简单方法 1:控制台颜色(控制台屏幕缓冲区)>易于由微软文档使用。 控制台屏幕缓冲区⭐⭐⭐
#include <stdio.h>
#include <windows.h>
/* Console colors only RGB(Red, Green, Blue, Normal) */
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 BLUE text, isn't it?\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_RED);
printf("This is some nice RED text, isn't it?\n");
SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN);
printf("This is some nice GREEN text, isn't it?");
/* Restore original attributes */
SetConsoleTextAttribute(hConsole, saved_attributes);
printf("\nBack to normal");
return 0;
}
2:ANSI 颜色 ANSI 颜色⭐⭐⭐⭐⭐
#include <stdio.h>
#define AC_BLACK "\x1b[30m"
#define AC_RED "\x1b[31m"
#define AC_GREEN "\x1b[32m"
#define AC_YELLOW "\x1b[33m"
#define AC_BLUE "\x1b[34m"
#define AC_MAGENTA "\x1b[35m"
#define AC_CYAN "\x1b[36m"
#define AC_WHITE "\x1b[37m"
#define AC_NORMAL "\x1b[m"
int main()
{
printf("Color test:\n");
printf("%sThis is red text\n",AC_RED);
printf("%sThis is red text\n",AC_GREEN);
printf("%sThis is red text\n",AC_BLUE);
printf("%sThis is red text%s\n",AC_YELLOW, AC_NORMAL);
// AC_NORMAL to default color...
printf("\nANSII COLORS 8\n");
printf("End test\n");
return(0);
}