DEV Community

Discussion on: CPP output coloured text in console

Collapse
 
willkirkby profile image
Will Kirkby

Naturally, this doesn't work on Windows :)

Collapse
 
deebuls profile image
Deebul Nair

Yes true !! Only works for linux. Not touched windows for years.

Collapse
 
christianparpart profile image
Christian Parpart

You actually can use this on windows, but you've to tell conhost about that.

bool initializeConsoleVT()
{
    if (auto handle = GetStdHandle(STD_OUTPUT_HANDLE); handle)
    {
        DWORD mode = 0;
        if (GetConsoleMode(handle, &mode) != 0)
        {
            mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
            if (SetConsoleMode(handle, mode))
                return true;
        }
    }
    return false;
}