【问题标题】:C color text in terminal applications in windowsWindows终端应用程序中的C颜色文本
【发布时间】:2012-02-08 23:45:09
【问题描述】:

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

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

请使用 C,而不是 C++

【问题讨论】:

  • 我在 Windows 上的 git bashMSYS2 上都使用 ANSI 转义码,效果很好。

标签: c windows cmd command-prompt textcolor


【解决方案1】:

由于您需要特定于 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;
}

有关可用属性的更多信息,请查看here

希望这会有所帮助! :)

【讨论】:

  • 非常感谢,效果很好,只是想知道如何将其设置回默认的浅灰色?非常感谢!
  • @JoeDF 为此,您需要使用GetConsoleScreenBufferInfo() 读取原始属性,将它们存储在变量中,然后在完成后恢复它们。我已经更新了答案以显示如何做到这一点。 :)
  • @JoeDF 没问题。如果您认为您的问题已得到解答,请随时 accept 回答... :)
【解决方案2】:
【解决方案3】:

我添加了一个简单的脚本来实现这一点,一些注意事项:

#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;

}

文档

【讨论】:

    猜你喜欢
    • 2011-04-04
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2023-03-18
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多