//**************************** BEGIN CONSOLE.H ********************************#ifndef ECONSOLE_H#define ECONSOLE_H#include <windows.h>//==============================================================================// class Console - Eric Tetz 10/5/99//// Encapsulates the Windows console API (some of it).//// Each process can have one and only one console. For this reason, all Console// members are static. If your program is already a console app, you can freely// call any method of this class. If your app is NOT a console app, you must// first call Alloc() to create a console for your process and redirect IO to// it. You may call Free() to detach your process from it''s console.////==============================================================================/* bool SetTitle (LPCSTR sTitle) bool SetSize (int columns, int lines) bool GetSize (int* pcolumns, int* plines) bool SetCursorPos (int x, int y) bool GetCursorPos (int* px, int* py) bool SetCursorSize (DWORD dwPercentShown, bool bVisible = false) bool SetTextColor (Color FgColor, Color BgColor = Black) bool Clear()*/class Console{public: enum Color { Black = 0, Grey = FOREGROUND_INTENSITY, LightGrey = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE, White = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, Blue = FOREGROUND_BLUE, Green = FOREGROUND_GREEN, Cyan = FOREGROUND_GREEN | FOREGROUND_BLUE, Red = FOREGROUND_RED, Purple = FOREGROUND_RED | FOREGROUND_BLUE, LightBlue = FOREGROUND_BLUE | FOREGROUND_INTENSITY, LightGreen = FOREGROUND_GREEN | FOREGROUND_INTENSITY, LightCyan = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY, LightRed = FOREGROUND_RED | FOREGROUND_INTENSITY, LightPurple = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY, Orange = FOREGROUND_RED | FOREGROUND_GREEN, Yellow = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY, };public: static bool Alloc(); static bool Free(); static bool SetTitle (LPCSTR sTitle); static bool SetSize (int columns, int lines); static bool GetSize (int * pcolumns, int * plines); static bool SetCursorPos (int x, int y); static bool GetCursorPos (int * px, int * py); static bool SetCursorSize (DWORD dwPercentShown, bool bVisible = false); static bool SetTextColor (Color FgColor, Color BgColor = Black); static bool Clear();protected: static bool RedirectIoToConsole ();};#endif //ECONSOLE_H//**************************** BEGIN CONSOLE.H ********************************//*************************** BEGIN CONSOLE.CPP *******************************#include "stdafx.h" // precompiled header directive#include <iostream>#include <wincon.h>#include <fcntl.h>#include <io.h>#include "eConsole.h"using std::ios;extern "C" void ConsoleAlloc(){ Console::Alloc();}bool Console::Alloc(){ // If this is already a console app we don''t need to call // RedirectToConsole(). AllocConsole() fails if this process // already has a console. return (AllocConsole() && RedirectIoToConsole());}bool Console::Free(){ return FreeConsole();}bool Console::SetTitle (LPCSTR sTitle){ return SetConsoleTitle (sTitle);}bool Console::RedirectIoToConsole (){ HANDLE hStdHandle; int nConHandle; // redirect unbuffered STDOUT to the console hStdHandle = GetStdHandle (STD_OUTPUT_HANDLE); nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT); *stdout = *_fdopen (nConHandle, "w"); setvbuf (stdout, NULL, _IONBF, 0); // redirect unbuffered STDIN to the console hStdHandle = GetStdHandle (STD_INPUT_HANDLE); nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT); *stdin = *_fdopen (nConHandle, "r"); setvbuf (stdin, NULL, _IONBF, 0); // redirect unbuffered STDERR to the console hStdHandle = GetStdHandle (STD_ERROR_HANDLE); nConHandle = _open_osfhandle ((long)hStdHandle, _O_TEXT); *stderr = *_fdopen (nConHandle, "w"); setvbuf (stderr, NULL, _IONBF, 0); // make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog // point to console as well ios::sync_with_stdio(); return true;}bool Console::GetSize (int * pcolumns, int * plines){ HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO coninfo; if (GetConsoleScreenBufferInfo (hconsole, &coninfo)) { *pcolumns = coninfo.dwSize.X; *plines = coninfo.dwSize.Y; return true; } else { return false; }}bool Console::SetSize (int columns, int lines){ HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE); COORD size = { columns, lines }; return SetConsoleScreenBufferSize (hconsole, size);}bool Console::SetTextColor (Color FgColor, Color BgColor /*= Black*/){ HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE); return (SetConsoleTextAttribute (hconsole, FgColor | BgColor << 4) == TRUE);}bool Console::Clear (){ HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE); // get the number of character cells in the current buffer CONSOLE_SCREEN_BUFFER_INFO csbi; if (!GetConsoleScreenBufferInfo (hconsole, &csbi)) return false; COORD coordScreen = { 0, 0 }; // here''s where we''ll home the cursor DWORD cCharsWritten; // number of chars written by console output routines DWORD dwConSize = csbi.dwSize.X * csbi.dwSize.Y; // fill the entire screen with blanks return (FillConsoleOutputCharacter (hconsole, '' '', dwConSize, coordScreen, &cCharsWritten) && FillConsoleOutputAttribute (hconsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten) && SetConsoleCursorPosition (hconsole, coordScreen));}bool Console::SetCursorPos (int x, int y){ HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE); COORD dwCursorPosition = { x, y }; return (SetConsoleCursorPosition (hconsole, dwCursorPosition) == TRUE);}bool Console::GetCursorPos (int * px, int * py){ HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO coninfo; if (GetConsoleScreenBufferInfo (hconsole, &coninfo)) { *px = coninfo.dwCursorPosition.X; *py = coninfo.dwCursorPosition.Y; return true; } else { return false; }}bool Console::SetCursorSize (DWORD dwPercentShown, bool bVisible /*=false*/){ HANDLE hconsole = GetStdHandle (STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO CursorInfo = { dwPercentShown, bVisible }; return (SetConsoleCursorInfo (hconsole, &CursorInfo) == TRUE);}//*************************** END CONSOLE.CPP **********************************
http://www.gdarchive.net/druidgames/