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
|
#include <iostream>
using namespace std;
#include <windows.h>
struct console_t
{
DWORD mode;
HANDLE hstdin;
HANDLE hstdout;
console_t()
{
hstdin = GetStdHandle( STD_INPUT_HANDLE );
hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
GetConsoleMode( hstdin, &mode );
}
~console_t()
{
if (hstdin != INVALID_HANDLE_VALUE)
SetConsoleMode( hstdin, mode );
}
bool iskeypressed( unsigned timeout_ms = 0 ) const
{
return WaitForSingleObject( hstdin, timeout_ms ) == WAIT_OBJECT_0;
}
int getkeypress() const
{
INPUT_RECORD inrec;
DWORD count;
if (hstdin == INVALID_HANDLE_VALUE) return -1;
SetConsoleMode( hstdin, 0 );
do ReadConsoleInput( hstdin, &inrec, 1, &count );
while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);
SetConsoleMode( hstdin, mode );
return inrec.Event.KeyEvent.wVirtualKeyCode;
}
void gotoxy( int column, int line )
{
COORD coord;
coord.X = column;
coord.Y = line;
SetConsoleCursorPosition( hstdout, coord );
}
void clearscreen()
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
if (hstdout == INVALID_HANDLE_VALUE) return;
if (!GetConsoleScreenBufferInfo( hstdout, &csbi )) return;
cellCount = csbi.dwSize.X * csbi.dwSize.Y;
if (!FillConsoleOutputCharacter(
hstdout,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
if (!FillConsoleOutputAttribute(
hstdout,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
SetConsoleCursorPosition( hstdout, homeCoords );
}
};
int main()
{
console_t console;
int key = 0;
console.clearscreen();
while (key != '3')
{
console.gotoxy( 0, 0 );
cout << "\n"
"1. Print Greeting\n"
"2. Print Farewell\n"
"3. Exit.\n"
"> ";
key = console.getkeypress();
switch (key)
{
case '1': cout << "\n\nHello world!\n"; break;
case '2': cout << "\n\nGood-bye! \n"; break;
case '3': cout << "\n\nDone. \n"; break;
}
}
return 0;
}
|