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++

Joe DF
  • 5,438
  • 6
  • 41
  • 63

3 Answers3

42

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! :)

Miguel
  • 1,966
  • 2
  • 18
  • 32
  • Thanks a lot, it works great, just wondering how to set it back to the default light gray? Thanks a lot! – Joe DF Feb 09 '12 at 01: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 09 '12 at 02:36
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)

Community
  • 1
  • 1
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
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

Federico Baù
  • 6,013
  • 5
  • 30
  • 38