Подтвердить что ты не робот

C цветной текст в терминальных приложениях в окнах

Я знаю "textcolor();" для С++, и я видел методы для unix... но есть ли способ и для окон?

#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, пожалуйста, не С++

4b9b3361

Ответ 1

Поскольку вы хотите использовать специальное решение 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;
}

Для получения дополнительной информации о доступных атрибутах, посмотрите здесь.

Надеюсь, это поможет!:)

Ответ 3

Работает второй с "include windows.h". Другой, вероятно, является началом