0

Possible Duplicate:
Colorize stdout output to Windows cmd.exe from console C++ app

I am using codeblocks on Windows XP and I need a way to output colored text, and maybe change the color of the background as well in a console application.
I tried the conio.h functions but they don't seem to be compatible with code blocks.

Community
  • 1
  • 1
Mihai
  • 307
  • 2
  • 6
  • 14
  • Out of curiosity, why did you change the answer? – Vite Falcon Feb 21 '12 at 16:41
  • Im kind of noobish.I thought you can accept several answers :D.So then i realised and i chose the short code.My program already has 4000 code lines(really) and I dont want to add to many of them.And i also want to understand what my codes does.Yours is harder for my to digest, since im not an advanced programmer. – Mihai Feb 21 '12 at 16:45

2 Answers2

2

It looks like you'll want to use some Windows API features to accomplish this.
If you were using Cygwin for windows, it'd be a bit easier.

Here's an example courtesy of daniweb:

// color your text in Windows console mode
// colors are 0=black 1=blue 2=green and so on to 15=white
// colorattribute = foreground + background * 16
// to get red text on yellow use 4 + 14*16 = 228
// light red on yellow would be 12 + 14*16 = 236
// a Dev-C++ tested console application by vegaseat 07nov2004

#include <iostream>
#include <windows.h> // WinApi header

using namespace std; // std::cout, std::cin

int main()
{
    HANDLE hConsole;
    int k;

    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    // you can loop k higher to see more color choices
    for(k = 1; k < 255; k++)
    {
        // pick the colorattribute k you want
        SetConsoleTextAttribute(hConsole, k);
        cout << k << " I want to be nice today!" << endl;
    }

    cin.get(); // wait
    return 0;
}
Mr. Llama
  • 20,202
  • 2
  • 62
  • 115
2

This piece of code might help:

WinConsole.h

#pragma once

typedef void* HANDLE;

class WinConsole
{
public:
    WinConsole(void);
    ~WinConsole(void);

    void SetColour(WORD colour);

    WORD GetDefaultColour() const;

    void Reset();
private:
    HANDLE fConsoleHandle;
    CONSOLE_SCREEN_BUFFER_INFO fDefaultScreenBufferInfo;
};

WinConsole.cpp

#include "WinConsole.h"
#define WIN32_LEAN_AND_MEAN
#define <Windows.h>

WinConsole::WinConsole(void)
{
    fConsoleHandle = ::GetStdHandle(STD_OUTPUT_HANDLE);
    if (INVALID_HANDLE_VALUE != fConsoleHandle)
    {
        ::GetConsoleScreenBufferInfo(fConsoleHandle, &fDefaultScreenBufferInfo);
    }
}

WinConsole::~WinConsole(void)
{
}

void WinConsole::SetColour( WORD colour )
{
    if (INVALID_HANDLE_VALUE != fConsoleHandle)
    {
        ::CONSOLE_SCREEN_BUFFER_INFO info = { sizeof(CONSOLE_SCREEN_BUFFER_INFO), 0 };
        if(::GetConsoleScreenBufferInfo(fConsoleHandle, &info))
        {
            ::SetConsoleTextAttribute(fConsoleHandle, (info.wAttributes & 0xff00)|colour);
        }
    }
}

void WinConsole::Reset()
{
    if (INVALID_HANDLE_VALUE != fConsoleHandle)
    {
        ::SetConsoleTextAttribute(fConsoleHandle, fDefaultScreenBufferInfo.wAttributes);
    }
}

WORD WinConsole::GetDefaultColour() const
{
    if (INVALID_HANDLE_VALUE != fConsoleHandle)
    {
        return (WORD)(fDefaultScreenBufferInfo.wAttributes & 0x00ff);
    }
    return e_FGRed | e_FGGreen | e_FGBlue;
}

Usage:

WinConsole console;
console.SetColour(FOREGROUND_RED|BACKGROUND_BLUE); // Superman style ;)
Vite Falcon
  • 6,575
  • 3
  • 30
  • 48