Helper Class for Console Functions

Have you tried to print colored text in a console or put the text at a specific position? You know then that STL or CRT does not provide that functionality.
Windows API however, contains a set of functions for working with console windows. The list of these functions is available in MSDN.

However, to simplify the use of these functions I have created a template, wrapper class that provides methods and overloaded operators to:

  • change the color for text and background
  • set the cursor position
  • write to the console
  • read from the console

The class is called basic_console, and is a template class. There are two specializations, one for ANSI, called console, and one for UNICODE, called wconsole.

You can change the position on both horizontal and vertical or on only one of the two:

There is also an overloaded operator for changing the position, specified with a console_pos structure.

For the color, you can change the text and background color, using one of these functions:

Number of colors are limited, but we can combine them to produce more. Windows defines separate flags for the text and background colors.
I have created two classes text_color and back_color to define mnemonics for colors based on these flags.

Let’s see several examples. The first one, shows how to print text with different colors. Moreover, the colored text is always displayed starting with column 40.

Here is the output for this program

A second example shows how to print numbers starting from 1 to N and back to 1 to form a rhomb.

And here is the output, when the selected depth is 9:

The same can be achived using the overloaded operator<< for console_pos.

A third and last examples shows how to read a matrix on rows and columns.

Of course, this basic_console class does not carry all the possible functionality you can imagine, but it should be good enough for basic operations.

Here is the file with the source code.

8 Comments on "Helper Class for Console Functions"

  1. Just to clarify: doesn’t basic_console::UseDefaultColors() make both the foreground and the background black?
    From what I have seen, the least significant byte gives the color for foreground and background. Setting that to 0(zero) would make everything black, would it not?

    Thank you in advance.

  2. Thank you for pointing out the error in the UseDefaultColors() function. There should be no mask there, because the point is to restore to the original attributes retrieved with a call to GetConsoleScreenBufferInfo() in the constructor of class basic_console.

  3. I think I am too stupuid for this, but where is the funcion body of

    void SetForeColor(text_color::Color crText)
    void SetBackColor(bg_color::Color crBack)
    void SetColors(text_color::Color crText, bg_color::Color crBack)
    void SetColors(WORD colors)
    void SetColors(WORD crText, WORD crBack)
    void UseDefaultColors()

    What is WORD

    And why does my compiler not recognize “basic_console”?


Leave a Reply

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