CConsole: A wrapper for Console APIs

This class is a wrapper for Console APIs. While browsing through the newsgroups, I realized that there are many developers who use console to output their diagnostic messages and to take input from the user in order to change the behavior during debug. So, Here it is. This class encompasses of one really important function RedirectToConsole () whose code is I got from Mr.Andrew Tucker's article in WDJ Dec 97 and also from a KB article (Q126628). Along with that I have added some more functions to make life easier.

Following is the list of functions available.

//Constructor & Destructor

CConsole
(); //This creates an empty class. This will not create the console.

CConsole (BOOL); // This creates the class and a console if TRUE is passed as an argument.

~CConsole (); //destroys the class and the console if created.

//Properties

short GetNumberOfLines();

short SetNumberOfLines (short sLines);

These functions set/get the number of lines you want in the console. SetNumberOfLines () returns the old value.

short GetNumberOfColumns ();

short SetNumberOfColumns (short sColumns);

These functions set/get the number of columns you want in the console. SetNumberOfColumns () returns the old value.

WORD GetAttributes ();

WORD SetAttributes (WORD wAttrib,short NumChars = 0);

These functions set/get the attributes you want in the console. SetAttributes () returns the old value. If you set the NumChars to other value than 0, It will set the attributes only to that many characters. Otherwise, It will be set from the cursor position to end of the console. The values that can be used is given below.

short SetMaxLinesInWindow (short maxLines);

short GetMaxLinesInWindow();

Get/Sets the maximum lines in the window..

//Methods

void RedirectToConsole (WORD wFlags);

Redirect the console io functions to console, i.e. printf(), scanf(), cout(), cin() etc. wFlags is not used (yet).J

BOOL SetupConsole(WORD wFlags);

Sets up the console to given flag. Internal function..

HANDLE GetHandle (DWORD dwFlag);

This returns the specified handle STD_OUTPUT_HANDLE, STD_INPUT_HANDLE and STD_ERROR_HANDLE are the valid dwFlag values.

BOOL Clear ();

Clears the screen with the specified attributes. Used a KB article to get it working.

WORD GetSettings (WORD wFlags);

Get the settings for a given flag.

BOOL CreateConsole ();

Creates the console.

BOOL DestroyConsole ();

Destroys the console.

 

//Attribute Values

BACKGROUD_BLUE,BACKGROUND_GREEN,BACKGROUND_RED,BACKGROUND_INTENSITY

FOREGROUND_BLUE,FOREGROUND_GREEN,FOREGROUND_RED,FOREGROUND_INTENSITY

 

//Flags to be used in SetupConsole and GetSettings

SC_LINES,SC_COLUMNS,SC_MAXLINES and SC_ATTRIB.

NOTE: If you are using it in MFC application with precompiled headers, you need to #include "stdafx.h" in the beginning of Console.cpp. Otherwise, you might see a error C1010 while compiling.

Download file. It contains a small test.cpp which shows how to use this class. Add the files to a win32 application's project and compile. That should run without complaining.

Have Fun..



Comments

  • How can I Change the color of Console window?

    Posted by Legacy on 10/10/2000 12:00am

    Originally posted by: Sneha

    How can I Change the color of Console window? If you know, Please mail me. It's a urgent.
    

    Reply
  • App terminates if console window is closed by user using the system menu.

    Posted by Legacy on 12/23/1998 12:00am

    Originally posted by: Roy Kiesler

    How can I prevent my app from terminating in this situation?

    Roy

    Reply
  • Programmer

    Posted by Legacy on 09/28/1998 12:00am

    Originally posted by: Max Leung

    This is an excellent solution for adding proper console support to MFC applications. A simple call to ::AllocConsole() and then using this class works beautifully, and you don't need to convert your application into a console application either!

    Thanks very much!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud-based suite of productivity/ collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

  • Live Event Date: July 28, 2016 @ 1:00 p.m. ET / 10:00 a.m. PT Jepsen tests are third-party tests for distributed databases that validate vendors' guarantees about how they perform under various failure scenarios, especially network partitions. These have proven their value as tools in any distributed system tester's arsenal. When the creator of Jepsen, Kyle Kingsbury, started his Jepsen-for-Hire business last fall, VoltDB immediately got in line, and over the past two months, our solution was given the most …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date