I would like to say hello, first of all.
I have been attempting to change the color of a character on my program for some time now.
The characters in question will represent lava tiles with the letter "L" in my program.
I would like to know how I can change individual character color in a Console program without having the change affecting all the characters.
Any ideas?
(The platform in question is windows console.)

Recommended Answers

All 3 Replies

You can switch the console text color at will quite easily. Just change it to the desired color before printing the character, then change it back after printing the character:

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

class TextAttr {
    friend std::ostream& operator<<(std::ostream& out, TextAttr attr)
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr.value);
        return out;
    }
public:
    explicit TextAttr(WORD attributes): value(attributes) {}
private:
    WORD value;
};

#define FOREGROUND_WHITE (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

int main()
{
    std::cout << "Regular text\n"
              << TextAttr(FOREGROUND_INTENSITY | FOREGROUND_GREEN)
              << "Bright green text\n"
              << TextAttr(FOREGROUND_WHITE)
              << "Back to regular\n";
}

This works just fine. Thanks.

Sorry but There is only Blue, Red, and Green for the color of the Back/Fore-ground?

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.