Description:
This class is a simple wrapper for some console functions.
The main goal is to keep it that simple so that even
beginners have no problems implementing it in their apps.
You can write debug outout or anything else you wish into
the console in different colors.
Usage:
Copy Console.cpp (this file) and Console.h into the directory
of your application. Insert them into the workspace if you wish.
Next, add the line '#include "Console.h"' into one of your source
files and add a member variable of tyle CConsole.
Copyright & disclaimer:
Do want you want to do with this class: Modify it, extend it to your
needs. Use it in both Freeware, Shareware or commercial applications,
but make sure to leave these comments at the top of this source file
intact and don't remove my name and E-mail address.
*/
#include "stdafx.h"
#include "Console.h"
// macro for the Clear() function
#define ERR(bSuccess) { if(!bSuccess) return; }
// macro to check whether hConsole is valid
#define CHECK(hHandle) { if(hHandle == NULL) return; };
// no color specified, reset to defaults (white font on black background)
if(wColor != NULL)
SetConsoleTextAttribute(hConsole, wColor );
// change font and/or background color
else
SetConsoleTextAttribute(hConsole, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE); // white text on black bg
}
// if not parameter set, write a new line
if(szOutput == NULL)
sprintf(out,"\n");
// process arguments
else
{
va_start(va, szOutput);
vsprintf(out, szOutput, va);
va_end(va);
}
// write to the console
WriteConsole(hConsole,out,strlen(out),&dwWritten,0);
}
char* CConsole::GetTitle()
{
// get the title of our console and return it
static char szWindowTitle[256] = "";
GetConsoleTitle(szWindowTitle,sizeof(szWindowTitle));
// get out console's HWND and show/hide the console
HWND hWnd = GetHWND();
if(hWnd != NULL)
ShowWindow(hWnd, SW_HIDE ? SW_SHOW : bShow);
}
void CConsole::DisableClose()
{
CHECK(hConsole);
HWND hWnd = GetHWND();
// disable the [x] button if we found our console
if(hWnd != NULL)
{
HMENU hMenu = GetSystemMenu(hWnd,0);
if(hMenu != NULL)
{
DeleteMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
DrawMenuBar(hWnd);
}
}
}
void CConsole::Clear()
{
CHECK(hConsole);
/***************************************/
// This code is from one of Microsoft's
// knowledge base articles, you can find it at
// http://support.microsoft.com/default.aspx?scid=KB;EN-US;q99261&
/***************************************/
COORD coordScreen = { 0, 0 };
BOOL bSuccess;
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
DWORD dwConSize;
/* get the number of character cells in the current buffer */