cppconlib: A C++ library for working with the Windows console

This project has been moved to GitHub.

New location: https://github.com/mariusbancila/cppconlib

Many years ago I published on my blog a helper class for working with the Windows console that was wrapping the Windows console API. Looking back at it I realized it was a pretty naive implementation. So I decided to start a new and make something more flexible and easier to use. Hopefully, I was more successful. The result is a small C++ template library called cppconlib, available on codeplex.

cppconlib is built with C++11 features and requires Visual Studio 2012 or newer. The library is available in a single header called conmanip.h and provides a set of helper classes, functions and constants for manipulating a Windows console (using the Windows console functions). The library features the following components:

  • console_context<T>: represents a context object for console operations; its main purpose is restoring console settings; typedefs for the three consoles are available (console_in_context, console_out_context and console_err_context)
  • console<T>: represents a console objects providing operations such as changing the foreground and background colors, the input mode, screen buffer size, title, and others; typedefs for the three consoles are available (console_in, console_out and console_err)
  • manipulating functions that can be used with cout/wcout and cin/wcin: settextcolor()/restoretextcolor(), setbgcolor()/restorebgcolor(), setcolors(), setmode()/clearmode(), setposx()/setposy()/setpos().

The library can be downloaded from here. Detailed documentation is available here.

cppconlib

Examples

The following example prints some text in custom colors and then reads text in a different set of colors.

cppconlib2

The following code prints a rhomb to the console:

cppconlib3

For more details and updates check the project at codeplex: https://cppconlib.codeplex.com.

UPDATE: A NuGet package for cppconlib is available.

1 Comment on "cppconlib: A C++ library for working with the Windows console"


  1. Hi Marius, I use your library and had to make this one addition to hide the cursor while running. Might I suggest including it in a future release.

    Another static global function:

    static void _setcursorvis(HANDLE const console, bool visible)
    {
    CONSOLE_CURSOR_INFO cur;
    ::GetConsoleCursorInfo(console, &cur);
    cur.bVisible = false;
    SetConsoleCursorInfo(console, &cur);
    }

    Then in the console class:

    void setcursorvis(bool visible)
    {
    _details::_setcursorvis(context.handle, visible);
    }

    Usage:

    console_out_context ctxout;
    console_out conout(ctxout);
    conout.setcursorvis(false);

    Thanks for the great library.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.