This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| Dynamic Console Wrapper Submitted by

A while ago, there was some Tip Of The Days covering up dynamic console windows and how to use them with coloring etc.
Until now, I found it rather useless, but now I (finally?) got a good use for them, and I decided to wrap up the functionality within a class (I find this a bit more useful).
To some of you, this might look as nonsense, since it only wraps functionality, but I find it useful, and if you do, feel free to use it as you want. It's all yours.

Currently browsing [dynconsole.zip] (2,459 bytes) - [win32util_console.h] - (2,902 bytes)

    #ifndef CONSOLE_H
    #define CONSOLE_H

    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>

    namespace Win32Util {

    	/**
    	 * @class  Console
    	 * @brief  Just opens a dynamic console and makes you write to it in colors etc.
    	 */
    	class Console {

    	private:

    		// Inited?
    		bool   mInited;

    		// Console specific variables
    		HANDLE mHandle;
    		int    mWidth;
    		int    mHeight;
    		CONSOLE_CURSOR_INFO mCCI;

    	public:

    		// Color scheme, just use Console::black for istance, then calling set Color
    		enum Color {
    			black       = 0,
    			blue        = 1,
    			green       = 2,
    			cyan        = 3,
    			red         = 4,
    			purple      = 5,
    			brown       = 6,
    			gray        = 8,
    			light_gray  = 7,
    			light_blue  = 9,
    			light_green = 10,
    			light_cyan  = 11,
    			light_red   = 12,
    			pink        = 13,
    			yellow      = 14,
    			white       = 15,
    		};

    		/**
    		 * Constuctor
    		 */
    		Console();

    		/**
    		 * Destructor
    		 */
    		~Console();

    		/**
    		 * Opens a console window
    		 *
    		 * @param  pWidth  width of buffer
    		 * @param  pHeight height of buffer
    		 */
    		void open(const int pWidth=0, const int pHeight=0);

    		/**
    		 * Closes the console window
    		 */
    		void close();

    		/**
    		 * Clears a specific number of rows with the color specified
    		 * in setColor(...).
    		 *
    		 * @param  pRows  Numer of rows (from top) to be cleared.
    		 */
    		void clear(const int pRows=-1);

    		/**
    		 * Shows text carret
    		 */
    		void showCursor();

    		/**
    		 * Hides text carret
    		 */
    		void hideCursor();

    		/**
    		 * Set size of cursor, the height from bottom.
    		 *
    		 * @param  pSize  Size of carret in percent
    		 */
    		void setCursorSize(const int pSize=100);

    		/**
    		 * Sets title of console window
    		 */
    		void setTitle(const char* pTitle);

    		/**
    		 * Set color to use with the output commands
    		 *
    		 * @param  pForegroundColor  Color of text
    		 * @param  pBackgroundColor  Color behind the text
    		 */
    		void setColor(const int pForegroundColor=light_green, const int pBackgroundColor=blue);

    		/**
    		 * Move cursor (carret) to new position.
    		 *
    		 * @param  pX  New x (horisontal) position
    		 * @param  pY  New y (vertical) position
    		 */
    		void locate(const int pX=0, const int pY=0);

    		/**
    		 * Writes a message on screen at carret position.
    		 * Note! Carret position is modified by this operation.
    		 *
    		 * @param  pMessage  The message in prinft kind of syntax
    		 */
    		void write(const char* pMessage, ...);

    		/**
    		 * Writes a message on screen at specified position.
    		 * Note! Carret position is modified by this operation.
    		 *
    		 * @param  pX        The x (horisontal) position
    		 * @param  pY        The y (vertical) position
    		 * @param  pMessage  The message in prinft kind of syntax
    		 */
    		void write(const int pX, const int pY, const char* pMessage, ...);

    	};
    }

    #endif

Currently browsing [dynconsole.zip] (2,459 bytes) - [win32util_console.cpp] - (4,524 bytes)

    #include "win32util_console.h"
    #include <stdarg.h>
    #include <stdio.h>

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Win32Util::Console::Console() {

    	// Set inited to false
    	mInited=false;
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    Win32Util::Console::~Console() {

    	// If window is open, close
    	if (mInited) close();
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::open(const int pWidth, const int pHeight) {

    	mWidth  = pWidth;
    	mHeight = pHeight;

    	// Opens a console window
    	AllocConsole();
    	mHandle = GetStdHandle( STD_OUTPUT_HANDLE );

    	// If pWidth and pHeight is specified
    	if (pWidth*pHeight>0) {

    		// Set up a small rect
    		SMALL_RECT rect={0,0,pWidth,pHeight};

    		// Change size and 'absolue' the window (fixed size)
    		SetConsoleWindowInfo(mHandle, true, &rect);

    		// Set buffer equal to size so that no scrollbars appears
    		COORD coord={pWidth, pHeight};
    		SetConsoleScreenBufferSize(mHandle, coord);
    	}

    	// Hide cursor as default action
    	hideCursor();
    	setCursorSize(100);

    	// Set default color
    	setColor();

    	// Set inited to true (since the windowed is opened)
    	mInited=true;
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::close() {

    	// Close the window
    	FreeConsole();

    	// Set inited to false (since the windowed is closed)
    	mInited=false;
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::clear(const int pRows) {

    	int rows = pRows;
    	if (pRows==-1) rows=mHeight;

    	// Go to upper left corner
    	locate(0,0);

    	// Make a string full of spaces equal the size of the window
    	char* empty = new char[mWidth];
    	memset(empty, ' ', mWidth);

    	// Write the string to the console
    	DWORD count;
    	for (int i=0; i<=rows; i++) {
    		WriteConsole(mHandle, empty, mWidth, &count, NULL);
    	}

    	// Clean up
    	delete empty;

    	// Go back to top left corner
    	locate(0,0);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::showCursor() {

    	// Set variables
    	mCCI.bVisible=TRUE;

    	// Set structure
    	SetConsoleCursorInfo(mHandle, &mCCI);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::hideCursor() {

    	// Set variables
    	mCCI.bVisible=FALSE;

    	// Set structure
    	SetConsoleCursorInfo(mHandle, &mCCI);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::setTitle(const char* pTitle) {

    	// Set title
    	SetConsoleTitle(pTitle);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::setCursorSize(const int pSize) {

    	// Set variable
    	mCCI.dwSize= pSize;

    	// Set structure
    	SetConsoleCursorInfo(mHandle, &mCCI);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::setColor(const int pForegroundColor, const int pBackgroundColor) {

    	// Set attribute (combine colors)
    	WORD attribute;
    	attribute = pForegroundColor | (pBackgroundColor<<4);

    	// Set color
    	SetConsoleTextAttribute(mHandle, attribute);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::locate(const int pX, const int pY) {

    	// Temporary
    	COORD coord={pX,pY};

    	// Set location
    	SetConsoleCursorPosition(mHandle, coord);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::write(const char* pMessage, ...) {

    	// Bind into one big string
    	char msg[1024];
    	va_list args;
    	va_start(args,pMessage);
    	vsprintf(msg,pMessage,args);

    	// Write to console
    	DWORD count;
    	WriteConsole(mHandle, msg, strlen(msg), &count, NULL);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    void Win32Util::Console::write(const int pX, const int pY, const char* pMessage, ...) {

    	// Move cursor
    	locate(pX, pY);

    	// Bind into one big string
    	char msg[1024];
    	va_list args;
    	va_start(args,pMessage);
    	vsprintf(msg,pMessage,args);

    	// Write to console
    	DWORD count;
    	WriteConsole(mHandle, msg, strlen(msg), &count, NULL);
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.