18

I know "textcolor();" is for C++ and i've seen methods for unix... but is there way for windows also?

#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");
}

I can't find any anything on the net... let's hope the good people from stack overflow can help :D

C please, not C++

1
  • I use ANSI escape codes on both git bash and MSYS2 on windows, it works fine. – ianfun Jul 29 at 13:10
40

Since you want a C and Windows specific solution, I'd recommend using the SetConsoleTextAttribute() function in the Win32 API. You'll need to grab a handle to the console, and then pass it with the appropriate attributes.

As a simple example:

/* 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;
}

For more info on the available attributes, look here.

Hope this helps! :)

3
  • Thanks a lot, it works great, just wondering how to set it back to the default light gray? Thanks a lot! – Joe DF Feb 9 '12 at 1:53
  • @JoeDF To do that you'll need to read in the original attributes with GetConsoleScreenBufferInfo(), store them in a variable, and then restore them when done. I've updated the answer to show how to do this. :) – Miguel Feb 9 '12 at 2:36
  • @JoeDF Not a problem. And if you consider that your question has been answered, feel free to accept an answer... :) – Miguel Feb 10 '12 at 1:41
3

Here you go: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686047(v=vs.85).aspx

You can see one usage of it right here on SO: What do this expression mean? (SetConsoleTextAttribute function in C)

0

I add a simple script that just achieve this, some considerations:

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

}

Using Defined Arguments

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

}

Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.