Any function for changing the text color in Microsoft Visual Studio 2008? Thanks.

What do you mean? Do you mean the colour of the code you write? :/

Yes, for example when executed.. Like cout with different color.

For use on Windows platform only:

#include <windows.h>

...

void SetColors( unsigned fg, unsigned bg )
  {
  SetConsoleTextAttribute(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    ((bg & 0xF) << 4) + (fg & 0xF)
    );
  }

See Microsoft's Console Functions for more.

If you plan to write cross-platform code then I would suggest looking at NCurses... let me know if that is the case.

Hope this helps.

I need to add this to the output statement? Because I tested it several times it's not working.

I need to add this to the output statement?

SetColors needs to be called with appropriate arguments before the output you want with a different color. It's also not a bad idea to save the current color attributes and restore them when you're done. That's pretty trivial with a sentry object:

#include <iostream>
#include <Windows.h>

class ConsoleColor {
    HANDLE console_handle;
    CONSOLE_SCREEN_BUFFER_INFO saved_info;
public:
    ConsoleColor(WORD foreground, WORD background);
    ~ConsoleColor();
};

ConsoleColor::ConsoleColor(WORD foreground, WORD background)
    : console_handle(GetStdHandle(STD_OUTPUT_HANDLE))
{
    GetConsoleScreenBufferInfo(console_handle, &saved_info);
    SetConsoleTextAttribute(console_handle, foreground | background);
}

ConsoleColor::~ConsoleColor()
{
    SetConsoleTextAttribute(console_handle, saved_info.wAttributes);
}

int main()
{
    std::cout<<"Hello, dull boring normal world\n";

    {
        // Create a color sentry with new colors
        ConsoleColor console_sentry(FOREGROUND_INTENSITY | FOREGROUND_GREEN, 0);

        std::cout<<"Hello, GREEN world!\n";

        // Sentry goes out of scope and restores original colors
    }

    std::cout<<"Back to normal\n";
}
commented: Nice +17

That's working. Really nice. Thanks for the code will examine it : ) Thanks again.

It's an interesting approach, but the scoped color idea is a bit awkward... I would do something like this:

#include <iostream>
#include <iomanip>
#include <windows.h>

struct Color
{
    int color;

    Color(int color_): color(color_) {}

    Color operator + (const Color & other) const { return Color(this->color | other.color); }
};

#define FORE_LIGHT(color) const Color _cfl##color = FOREGROUND_##color | FOREGROUND_INTENSITY;
#define BACK_LIGHT(color) const Color _cbl##color = BACKGROUND_##color | BACKGROUND_INTENSITY;
#define FORE_DARK(color)  const Color _cfd##color = FOREGROUND_##color;
#define BACK_DARK(color)  const Color _cbd##color = BACKGROUND_##color;

FORE_LIGHT(RED) FORE_LIGHT(GREEN) FORE_LIGHT(BLUE)
BACK_LIGHT(RED) BACK_LIGHT(GREEN) BACK_LIGHT(BLUE)
FORE_DARK(RED)  FORE_DARK(GREEN)  FORE_DARK(BLUE)
BACK_DARK(RED)  BACK_DARK(GREEN)  BACK_DARK(BLUE)

const Color _cdefault = _cfdRED + _cfdGREEN + _cfdBLUE;

std::ostream & operator << (std::ostream & os, Color color)
{
    return SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color.color), os;
}

int main()
{
    using std::cout; using std::setw; using std::endl;

    cout << std::setiosflags(std::ios_base::left);

    cout << "Hello, " << _cflRED                        << setw(6) << "Red"    << _cdefault << " World!" << endl;
    cout << "Hello, " <<           _cflGREEN            << setw(6) << "Green"  << _cdefault << " World!" << endl;
    cout << "Hello, " <<                       _cflBLUE << setw(6) << "Blue"   << _cdefault << " World!" << endl;
    cout << "Hello, " << _cflRED + _cflGREEN            << setw(6) << "Yellow" << _cdefault << " World!" << endl;
    cout << "Hello, " << _cflRED             + _cflBLUE << setw(6) << "Pink"   << _cdefault << " World!" << endl;
    cout << "Hello, " <<           _cflGREEN + _cflBLUE << setw(6) << "Cyan"   << _cdefault << " World!" << endl;
    cout << "Hello, " << _cblRED + _cblGREEN + _cblBLUE << setw(6) << "Black"  << _cdefault << " World!" << endl;

    cout << _cflRED + _cflGREEN + _cflBLUE << "\n(hit enter to quit...)" << _cdefault << endl;

    std::cin.get();

    return 0;
}
commented: nice :) +17
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.