• Welcome to Valhalla Legends Archive.
 

SetConsoleTextAttribute

Started by iNsaNe, September 18, 2008, 09:48 PM

Previous topic - Next topic

iNsaNe

I'm relatively new to C++ and was just wondering if it is possible to have 2+ colors on one line in the console? If so, how? For example:


SetConsoleTextAttribute(hStdOut, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
cout << "Green";

SetConsoleTextAttribute(hStdOut, FOREGROUND_RED | FOREGROUND_INTENSITY);
cout << "Red" << endl;


The console's foreground color for that whole line becomes red, where I want it to be green for "Green" and then red for "Red".

Barabajagal

Don't remember how DOS is, but Commodores (what I learned to program on) used special characters... Try looking into ASCII characters?

MyndFyre

#2
Yes, I was able to do it with this .NET program:

        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("Hello, ");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine("world");
            Console.ReadLine();
        }


I disassembled Console.ForegroundColor.set and it looks pretty equivalent to what you're doing:

    Win32Native.CONSOLE_SCREEN_BUFFER_INFO bufferInfo = GetBufferInfo(false, out flag);
    if (flag)
    {
        short attributes = (short) (bufferInfo.wAttributes & -241);
        attributes = (short) (((ushort) attributes) | ((ushort) color));
        Win32Native.SetConsoleTextAttribute(ConsoleOutputHandle, attributes);
    }

QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

MyndFyre

#3
As a note, I'm not sure what the overload of cout's shift operator calls, but the Windows docs say that console and text attributes do not affect the text written with low-level I/O functions such as WriteConsoleOutput or WriteConsoleOutputCharacter.  Source.

I'm not sure if that would have an impact on your program or not; just thought it was interesting.  My .NET program uses WriteFile to write to standard output.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

iNsaNe

I think the problem was that the color changes all the text after the cursor position. I think cout does not move the cursor as it outputs text which is why I could only make one color per line.

But WriteConsole does work, thanks!