1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
#include "Console.h"
HANDLE Console::cConsole = NULL;
BOOL WINAPI consoleHandler(DWORD dwCtrlType)
{
return true;
}
Console::Console(const char *title, int x, int y, int w, int h)
{
allocate();
if (!cConsole)
{
cConsole =
CreateConsoleScreenBuffer(GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_READ,
NULL, CONSOLE_TEXTMODE_BUFFER, 0);
}
COORD coord = {w, h};
SetConsoleScreenBufferSize(cConsole, coord);
SMALL_RECT conWin = {x, y, w, h};
SetConsoleWindowInfo(cConsole, true, &conWin);
SetConsoleCtrlHandler(consoleHandler, true);
getInfo();
setTitle(title);
SetConsoleActiveScreenBuffer(cConsole);
writeActive = true;
busy = false;
}
Console::~Console()
{
free();
CloseHandle(cConsole);
}
void Console::setCursor(int x, int y)
{
COORD coord = {x, y};
SetConsoleCursorPosition(cConsole, coord);
}
void Console::setColor(int textcolor, int backcolor)
{
WORD wAttributes = ((unsigned int) backcolor << 4) | (unsigned int) textcolor;
SetConsoleTextAttribute(cConsole, wAttributes);
}
void Console::clear(char ch)
{
DWORD cCharsWritten;
DWORD size = cCSBI.dwSize.X * cCSBI.dwSize.Y;
COORD coord = {0, 0};
FillConsoleOutputCharacter(cConsole, ch, size, coord, &cCharsWritten),
FillConsoleOutputAttribute(cConsole, cCSBI.wAttributes, size, coord, &cCharsWritten),
SetConsoleCursorPosition(cConsole, coord);
}
bool Console::flush()
{
SetConsoleActiveScreenBuffer(cConsole);
return (FlushConsoleInputBuffer(cConsole) == TRUE) ? true : false;
}
bool Console::allocate()
{
return (AllocConsole() == TRUE) ? true : false;
}
bool Console::free()
{
return (FreeConsole() == TRUE) ? true : false;
}
void Console::fillColor(int x, int y, int w, int h, char ch)
{
DWORD cCharsWritten;
COORD coord;
coord.X = x;
getInfo();
for (int i = 0; i < h; i++)
{
coord.Y = i + y;
if (ch)
FillConsoleOutputCharacter(cConsole, ch, w, coord, &cCharsWritten);
FillConsoleOutputAttribute(cConsole, cCSBI.wAttributes, w, coord, &cCharsWritten);
}
}
void Console::box(int x, int y, int w, int h)
{
int k;
if (!h && !w)
return;
setCursor(x, y);
if (!h)
{
for (k = 0; k < w; k++)
{
setCursor(x + k, y);
write(196, false);
}
return;
}
else
if (!w)
{
for (k = 0; k < h; k++)
{
setCursor(x, y + k);
write(179, false);
}
return;
}
write(218, false);
for (k = 1; k < w - 1; k++)
{
setCursor(x + k, y);
write(196, false);
setCursor(x + k, y + h - 1);
write(196, false);
}
write(217, false);
for (k = 1; k < h - 1; k++)
{
setCursor(x, y + k);
write(179, false);
setCursor(x + w - 1, y + k);
write(179, false);
}
setCursor(x + w - 1, y);
write(191, false);
setCursor(x, y + h - 1);
write(192, false);
flush();
}
void Console::setCursorSize(unsigned size, bool visible)
{
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.bVisible = visible;
cursorInfo.dwSize = (size < 100) ? size : 100;
SetConsoleCursorInfo(cConsole, &cursorInfo);
}
bool Console::setTitle(const char *title)
{
return (SetConsoleTitle(title) == TRUE) ? true : false;
}
CONSOLE_SCREEN_BUFFER_INFO Console::getInfo()
{
GetConsoleScreenBufferInfo(cConsole, &cCSBI);
return cCSBI;
}
|