在Windows终端应用程序中设置C语言文本颜色

18

我知道"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");
}

我在网上找不到任何东西......希望来自stack overflow的好心人能够帮助:D

请用C语言,而不是C++。

4个回答

42

如果您需要一个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;
}

如果需要了解可用属性的更多信息,请查看此处

希望这能帮到您!:)


非常感谢,它运行得很好,只是想知道如何将其设置回默认的浅灰色?非常感谢! - Joe DF
@JoeDF 要做到这一点,您需要使用 GetConsoleScreenBufferInfo() 读取原始属性,将其存储在变量中,然后在完成后恢复它们。我已更新答案以显示如何执行此操作。 :) - Miguel

3

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;

}

文档


0
两种简单的方法 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 %s\n",AC_RED, AC_NORMAL);
    printf("%sThis is green text %s\n",AC_GREEN, AC_NORMAL);
    printf("%sThis is blue text %s\n",AC_BLUE, AC_NORMAL);
    printf("%sThis is yellow text %s\n",AC_YELLOW, AC_NORMAL);
    // AC_NORMAL to default color...
    printf("\nANSII COLORS 8\n");
    printf("End test\n");

    return(0);
}

1
你的回答可以通过提供更多支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到更多关于如何撰写好回答的信息。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接