Windows Console provides Object Oriented wrapper around Window's console.
#include "WindowsConsole.h"
using namespace WindowConsole;
int main() {
// create
WindowsConsole *console = new WindowsConsole();
console->Create();
// set title
console->SetCaption(L"My console");
// write on screen
console->Writeln(L"Hello world");
// wait for ESC key
while(console->ReadKey() != 27) { }
// clean up
console->Destroy();
delete console;
return 0;
}
Creates and sets up console. If you created console application project, then WindowsConsole object takes control over the existing console and releases control when you call Destroy() method. If you created windows application project, then WindowsConsole object creates new console and destroy it when you call Destroy() method.
WindowsConsole *console = new WindowsConsole();
console->Create();Destroys console's object and cleans up. If you created console application project, then WindowsConsole object takes control over the existing console and releases control when you call Destroy() method.If you created windows application project, then WindowsConsole object creates new console and destroy it when you call Destroy() method.
WindowsConsole *console = new WindowsConsole();
console->Create();
// ....
console->Destroy();
delete console;Write text on the console screen.
Method starts writing at the last cursor position and leaves cursor at the position right behind the last character of the displated text.
console->Write(L"Hello world");You can also specify color of the output text:
console->Write(L"Hello world", ConsoleColor::Red);As well as background color of the output text:
console->Write(L"Hello world", ConsoleColor::Red, ConsoleColor::White);Write text on the console screen and move cursor to new line.
Method starts writing at the last cursor position and move cursor to new line.
console->Writeln(L"Hello world");
console->Writeln(L"Hello world", ConsoleColor::Red);
console->Writeln(L"Hello world", ConsoleColor::Red, ConsoleColor::White);Reads characters from console input and save them in outBuffer.
Number of characters read from input must be smaller than input buffer size. You can get input buffer size using GetInputBufferSize() and set it with SetInputBufferSize().
std::wstring outBuffer;
console->Read(outBuffer);You can also specify color of the input text:
std::wstring outBuffer;
console->Read(outBuffer, ConsoleColor::Red);As well as background color of the input text:
std::wstring outBuffer;
console->Read(outBuffer, ConsoleColor::Red, ConsoleColor::White);Returns code of the key that is down.
// wait for ESC key
while(console->ReadKey() != 27) { }Clears console buffer.
If color is passed as argument, then a new backgorund color is set - it works like SetBackgorunColor().
console->Clear()
console->Clear(ConsoleColor::Red)Clears line where the cursor is.
console->Clearln()Sets new title. It is displayed in title bar of console window.
console->SetCaption(L"My title");Returns title of the current console window.
std::wstring = console->GetCaption();Moves cursor to fixed position.
The X and Y coordinates are zero based. Position {X = 0, Y = 0} is in upper-left corner of the window.
console->GotoXY(64, 32);Hides cursor.
console->HideCursor();Shows cursor.
console->ShowCursor();Sets cursor's size.
The cursor's size should be between 0 and 100. If it is greater than 100, size of the cursor will be cut to 100.
console->SetCursorSize(24);Returns size of the cursor. Returned size is in [0, 100].
char cursorSize = console->GetCursorSize();Hide console window.
console->HideConsoleWindow();Shows console window on the screen.
console->ShowConsoleWindow();Sets new background color of the console.
console->SetBackgroudColor(ConsoleColor::Red);Sets new color of the input font.
console->SetInputColor(ConsoleColor::Yellow);Sets new color of the output font.
console->SetOutputColor(ConsoleColor::White);Returns color of the backround.
ConsoleColor backgroundColor = console->GetBackgroudColor();Returns color of the input font.
ConsoleColor inputColor = console->GetInputColor();Returns color of the output font.
ConsoleColor outputColor = console->GetOutputColor();Resizes console window.
Size of the console window is limited and depends of user's screen resolution. You can resize window to specific size returned by GetLargestWindowSize(). If you try to create window that is too large, method will do nothing and return false.
console->SetWindowSize(128, 64);Returns console window size.
COORD size = console->GetWindowSize();Returns largest avalible console window size.
COORD largestSize = console->GetLargestWindowSize();Resizes console buffer.
Sometimes SetBufferSize() will also resize console window. This is necesary to resize buffer. For example, that happens when you create buffer smaller than window.
console->SetBufferSize(128, 64);Returns console buffer size.
COORD size = console->GetBufferSize();Set the input buffer size. This is number of characters that console will be able to read from input.
console-> SetInputBufferSize(1024);Returns input buffer size. This is number of character that can be read from input.
short inputBufferSize = console->GetInputBufferSize();Enables console echo.
When echo is enabled then charcters sent from input are displayed on the screen.
console->EnableEcho();Disables console echo.
When echo is disabled then charcters sent from input are NOT displayed on the screen.
console->DisableEcho();Returns handle (HWND) to console window.
HWND handle = console->GetWindowHandle();ConsoleColor contains following colors:
- Black
- DarkBlue,
- DarkGreen,
- DarkAqua,
- DarkRed
- DarkPurple,
- DarkYellow,
- DarkWhite,
- Grey,
- Blue,
- Green,
- Aqua,
- Red,
- Purple,
- Yellow,
- White,
- None
WindowsConsole is released under the MIT license. See LICENSE for details.