Code:
#ifndef IOBOX_H_INCLUDED
#define IOBOX_H_INCLUDED
#include <string>
#include <windows.h>
#include <conio.h>
typedef enum { // System key codes
ENTER = 13, BACKSPACE = 8, TAB = 9,
CTRL_TAB = 404, ESC = 27, UP_ARROW = 328,
DOWN_ARROW = 336, LEFT_ARROW = 331, RIGHT_ARROW = 333,
PAGE_UP = 329, PAGE_DOWN = 337, F1 = 315,
F2 = 316, F3 = 317, F4 = 318,
F5 = 319, F6 = 320, F7 = 321,
F8 = 322, F9 = 323, F10 = 324,
F11 = 325, F12 = 326
} SYSTEMKEYCODES;
struct BOXCOORDS {
BOXCOORDS() {}; // Default constructor
BOXCOORDS(int x, int y, int l):Xpos(x),Ypos(y),Length(l) {} //constructor to load coords
int Xpos, // X and
Ypos, // Y coords for the start position of the box
Length; // length (in characters) of the box
};
class ConsoleInputBox {
public : // Functions
ConsoleInputBox(char front, char rear):front_border(front),
rear_border(rear) {};
~ConsoleInputBox() {};
bool SetBox(const BOXCOORDS, const std::string);
std::string Display();
std::string Display(const std::string);
std::string Read();
wchar_t Get();
private : // Functions
static int GetCode() {
int ch = getch();
if ( ch == 0 || ch == 224 ) ch = 256 + getch();
return ch;
}
void RemoveTrailingSpaces(std::string&);
private : // Variables
BOXCOORDS box_coords;
short current_x_pos;
char front_border,
rear_border;
std::string title;
};
// Set the Coords, Prompt message, and border characters for the box
bool ConsoleInputBox::SetBox(const BOXCOORDS set_coords,
const std::string s) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD ConsoleSize = GetLargestConsoleWindowSize(hOut);
// Error checking for coord out-of-bounds
int left_side = set_coords.Xpos - s.length(),
right_side = set_coords.Xpos + set_coords.Length;
if (left_side >= 0 &&
right_side <= ConsoleSize.X &&
set_coords.Ypos >= 0) {
box_coords = set_coords;
title = s;
return true;
} else {
// Display error message
std::string str = "Prompt \'" + s + "\' puts coordinates past margin!!";
COORD pos;
pos.X = 0; pos.Y = 0;
LPDWORD NumWritten;
WriteConsoleOutputCharacter(hOut, "ERROR!!", 7, pos, NumWritten);
++pos.Y;
WriteConsoleOutputCharacter(hOut, str.c_str(), str.length(),
pos, NumWritten);
getch();
return false;
}
}
// Display the box
std::string ConsoleInputBox::Display() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD Position;
Position.X = ( (box_coords.Xpos - 1) - title.length() );
Position.Y = box_coords.Ypos;
SetConsoleCursorPosition(hOut, Position);
std::string space(box_coords.Length, ' ');
std::string str = title + front_border + space + rear_border;
return str;
}
// Display the box with optional information already in the box
std::string ConsoleInputBox::Display(const std::string s) {
std::string s2 = s;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD Position;
Position.X = ( (box_coords.Xpos - 1) - title.length() );
Position.Y = box_coords.Ypos;
SetConsoleCursorPosition(hOut, Position);
std::string space( (box_coords.Length - s.length() ), ' ');
if (s.length() > box_coords.Length)
s2 = s.substr(0, box_coords.Length);
std::string str = title + front_border + s2 + space + rear_border;
return str;
}
// Read the information from the box into a variable
std::string ConsoleInputBox::Read() {
char chStr[100];
COORD Pos;
DWORD NumRead;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
Pos.X = box_coords.Xpos; Pos.Y = box_coords.Ypos;
ReadConsoleOutputCharacter(hOut, chStr, box_coords.Length, Pos, &NumRead);
std::string str(chStr, 0, box_coords.Length);
std::string space(box_coords.Length, ' ');
if (str == space)
str.clear();
else
RemoveTrailingSpaces(str);
return str;
}
// Get user input into the box
wchar_t ConsoleInputBox::Get() {
enum {
_Key_BACKSPACE_ = 8,
_KEY_LEFT_ARROW_ = 331,
_KEY_RIGHT_ARROW_ = 333
};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
LPDWORD NumWritten;
COORD Position;
Position.X = box_coords.Xpos;
Position.Y = box_coords.Ypos;
SetConsoleCursorPosition(hOut, Position);
int current_x_pos = 0;
bool loop = true;
const int length = ( (box_coords.Xpos + box_coords.Length) - 1);
char ch;
wchar_t wch;
while(loop) {
wch = GetCode();
if (wch >= 32 && wch <= 126) {
if (current_x_pos < box_coords.Length) {
ch = wch;
WriteConsole(hOut, &ch, 1, NumWritten, NULL);
++current_x_pos;
}
} else switch (wch) {
case _KEY_LEFT_ARROW_ :
if (current_x_pos > 0) {
current_x_pos--;
Position.X = (box_coords.Xpos + current_x_pos);
SetConsoleCursorPosition(hOut, Position);
}
break;
case _KEY_RIGHT_ARROW_ :
if (current_x_pos < box_coords.Length) {
current_x_pos++;
Position.X = (box_coords.Xpos + current_x_pos);
SetConsoleCursorPosition(hOut, Position);
}
break;
case _KEY_BACKSPACE_ :
if (current_x_pos > 0) {
WriteConsole(hOut, "\b \b", 3, NumWritten, NULL);
current_x_pos--;
}
break;
default :
loop = false;
break;
}
}
return wch;
}
// Remove the trailing spaces in the variable
void ConsoleInputBox::RemoveTrailingSpaces(std::string& str) {
std::string::size_type index = str.find_last_not_of (" " , str.length() );
str.assign(str, 0, index + 1);
return;
}
#endif