none
Colourful console application

    Question

  • Hello, I am making a C++ console application in Visual Studio 2008 and I want to make the text colourful and I was to position the text where ever I wast on the screen.  Are there any libraries available to do this?

    I used to to this in DevC++ by linking to the conio2 library, it let me colour and position my text in the console, however it does not support VC++.

    Any help would be appriciated :D
    Sunday, June 22, 2008 11:46 AM

Answers

  • I don't have access to Vista right now so can't try it.  But something like this ought to work:

        CONSOLE_SCREEN_BUFFER_INFOEX info;
        info.cbSize = sizeof(info);
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfoEx(hConsole, &info);
        info.ColorTable[14] = RGB(255, 128, 0);  // Replace yellow
        SetConsoleScreenBufferInfoEx(hConsole, &info);
        SetConsoleTextAttribute(hConsole, FOREGROUNDINTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);

    Hope it works.

    Hans Passant.
    • Marked as answer by Yan-Fei Wei Saturday, June 28, 2008 8:17 AM
    Sunday, June 22, 2008 4:42 PM
    Moderator
  • They are well documented in the MSDN library.
    Hans Passant.
    • Marked as answer by Yan-Fei Wei Saturday, June 28, 2008 8:17 AM
    Sunday, June 22, 2008 3:06 PM
    Moderator

  • Quote>Are there any libraries available to do this?

    You might find JLib useful.

    http://www.codeproject.com/KB/winsdk/JLib.aspx

    - Wayne



    • Marked as answer by Yan-Fei Wei Saturday, June 28, 2008 8:16 AM
    Monday, June 23, 2008 12:52 AM

All replies

  • The CRT has no support.  The low-level Windows API functions are SetConsoleScreenBufferInfoEx() and SetConsoleTextAttribute().
    Hans Passant.
    • Proposed as answer by scorpion007 Sunday, June 22, 2008 1:10 PM
    • Unproposed as answer by e66n06 Sunday, June 22, 2008 5:17 PM
    Sunday, June 22, 2008 1:02 PM
    Moderator
  • How exactly would I go about using those functions?

    I can't find them in my Petzold book XD
    Sunday, June 22, 2008 2:52 PM
  • They are well documented in the MSDN library.
    Hans Passant.
    • Marked as answer by Yan-Fei Wei Saturday, June 28, 2008 8:17 AM
    Sunday, June 22, 2008 3:06 PM
    Moderator
  • I have spent the last half an hour looking through MSDN but I cant work out how to get SetConsoleScreenBufferInfoEx() to work..

    This is what I have been trying, but the GetConsoleScreenBufferInfoEx returns 0 as though it didn't work..

    hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFOEX csb=NULL;
    cout<<GetConsoleScreenBufferInfoEx(hConsoleOut,&csb)<<endl;
    cout<<csb.dwCursorPosition.X<<endl;
    cout<<csb.dwCursorPosition.Y<<endl;
    csb.dwCursorPosition.Y=10;
    csb.dwCursorPosition.X=10;
    cout<<SetConsoleScreenBufferInfoEx(hConsoleOut,&csb));

    Could someone help by showing an example please?

    I managed to get the colours working, so thank you :D  But how exactly do I change the intensities?

    I've managed

    SetConsoleTextAttribute(hConsoleOut, FOREGROUND_GREEN|FOREGROUND_BLUE);

    But how can I make other colours, say, orange?
    Sunday, June 22, 2008 4:05 PM
  • I don't have access to Vista right now so can't try it.  But something like this ought to work:

        CONSOLE_SCREEN_BUFFER_INFOEX info;
        info.cbSize = sizeof(info);
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        GetConsoleScreenBufferInfoEx(hConsole, &info);
        info.ColorTable[14] = RGB(255, 128, 0);  // Replace yellow
        SetConsoleScreenBufferInfoEx(hConsole, &info);
        SetConsoleTextAttribute(hConsole, FOREGROUNDINTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);

    Hope it works.

    Hans Passant.
    • Marked as answer by Yan-Fei Wei Saturday, June 28, 2008 8:17 AM
    Sunday, June 22, 2008 4:42 PM
    Moderator
  • Thank you, that kinda works, however each call to SetConsoleScreenBufferInfoEx(hConsole, &info); makes my console one character smaller in the X and Y directions ??

    Also if I do something like this:

        info.dwCursorPosition.X=10;
        info.dwCursorPosition.Y=10;
        SetConsoleScreenBufferInfoEx(hConsoleOut, &info);
        cout<<"hello"<<endl;

    is does not move the position of the "hello", it just prints where it normally would have done..

    Sorry to be such a pain, thanks for you help so far :D
    Sunday, June 22, 2008 5:16 PM

  • Quote>Are there any libraries available to do this?

    You might find JLib useful.

    http://www.codeproject.com/KB/winsdk/JLib.aspx

    - Wayne



    • Marked as answer by Yan-Fei Wei Saturday, June 28, 2008 8:16 AM
    Monday, June 23, 2008 12:52 AM
  • The cout method ignores the cursor position. Remember that the console is in effect a text stream. The cursor is moved in response to output to the stream, an API call by the application, or user interaction. The cursor displayed on the monitor is an effect intended to assist the user in knowing where the next INPUT (e.g. readline) will be displayed.

    If you want you app to display text as a specific location, you'll need to use a Console API call rather than the simple "cout" stream method.

    Visit the MSDN library page:

         /en-us/library/windows/desktop/ms687401(v=vs.85).aspx

    which is relative to:

         msdn.microsoft.com

    I would give you the complete URL but my MSDN account has not been verified yet and the Forum will not allow me to post complete URLs in replies. Sorry! ;)

    Hope this helps.

    Monday, July 08, 2013 1:36 AM
  • I would give you the complete URL but my MSDN account has not been verified yet and the Forum will not allow me to post complete URLs in replies. Sorry! ;)

    Hope this helps.

    Additional helpful posts to older threads are always welcome, but as you appear to be
    new to these forums you may not have noticed that the thread you posted to is five
    years old. It can pay to keep an eye on thread dates, so as not to waste time composing
    a reply to a "stale" thread.

    - Wayne

    Monday, July 08, 2013 7:08 AM