Colored text and background in C

Windows.h in C++... what's in C?

Page 1 of 1

1 Replies - 2394 Views - Last Post: 07 September 2008 - 06:10 AM Rate Topic: -----

#1 Schreib  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 26-August 08

Colored text and background in C

Post icon  Posted 06 September 2008 - 02:25 PM

Hi everyone.

Which library should I use, if I want to color my text and background in a console window? Much like the WriteConsole() etc. in C++? I only need it for a Windows platform.

Thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: Colored text and background in C

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Colored text and background in C

Posted 07 September 2008 - 06:10 AM

Try this

It's a snippet I wrote. :)

Specifically:
/*
 * The simplest way to have colourful text in the console
 * Author: Danny Battison
 * Contact: gabehabe@hotmail.com
 */

#include <iostream> /* Standard input/output stream */
#include <windows.h> /* Standard Windows API header */

void SetColour (int index) /* Set the colour, just pass the index */
{
    /* Pass the console handle and the index (param) to the Win API function */
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), index);
}

void ResetColour () /* Reset the colour to the default (white) */
{
    /* Pass 7 as the colour for the original white text colour */
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}

int main()
{
    std::cout << "This is the original colour for text..." << std::endl;
    SetColour (5); // anything after this call will be colourful
    std::cout << "This text should be colourful!" << std::endl;
    ResetColour (); // return the colour back to default
    std::cout << "This is the original colour for text again..." << std::endl;

    std::cin.get(); /* Hold the window open */
    return EXIT_SUCCESS; /* The program was executed successfully */
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1