Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

settextcolor()

shaolin007shaolin007 Posts: 1,018Member
Can you use this Windows API function in console mode? If so, then how do you obtain a handle to the running console window?

Comments

  • LundinLundin Posts: 3,711Member
    : Can you use this Windows API function in console mode? If so, then how do you obtain a handle to the running console window?


    C is case-sensitive.

    SetTextColor() is a function used to change text color of the device context. It only makes sense to use on windows that draw using the "GDI" (win api).

    The function you wish to use is SetConsoleTextAttribute().
  • shaolin007shaolin007 Posts: 1,018Member
    : : Can you use this Windows API function in console mode? If so, then how do you obtain a handle to the running console window?
    :
    :
    : C is case-sensitive.
    :
    : SetTextColor() is a function used to change text color of the device context. It only makes sense to use on windows that draw using the "GDI" (win api).
    :
    : The function you wish to use is SetConsoleTextAttribute().
    :
    [green]
    Thanks, I'll look into that.
    [/green]

  • shaolin007shaolin007 Posts: 1,018Member
    [green]
    Ok, I finally got it to work. Is there more colors I could use besides the standard 16? Is there a way to manipulate the palette in console mode? Took me a second to figure that I had to get a handle to stdout for it to work correctly. Here's what I did to test it.
    [/green]

    [code]
    #include
    #include

    int main ()
    {
    HANDLE hConsole;

    unsigned short int colors;


    hConsole=GetStdHandle(STD_OUTPUT_HANDLE);

    for ( colors=0; colors < 256; colors++)
    {

    SetConsoleTextAttribute(hConsole, colors);
    printf("%u ", colors);
    }

    SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
    printf("

    ");
    system("pause");

    return 0;
    }
    [/code]

  • LundinLundin Posts: 3,711Member
    : [green]
    : Ok, I finally got it to work. Is there more colors I could use besides the standard 16? Is there a way to manipulate the palette in console mode? Took me a second to figure that I had to get a handle to stdout for it to work correctly. Here's what I did to test it.
    : [/green]
    :
    : [code]
    : #include
    : #include
    :
    : int main ()
    : {
    : HANDLE hConsole;
    :
    : unsigned short int colors;
    :
    :
    : hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
    :
    : for ( colors=0; colors < 256; colors++)
    : {
    :
    : SetConsoleTextAttribute(hConsole, colors);
    : printf("%u ", colors);
    : }
    :
    : SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED);
    : printf("

    ");
    : system("pause");
    :
    : return 0;
    : }
    : [/code]
    :
    :


    I think 16 colors is what you get.
Sign In or Register to comment.