#include "stdafx.h" #include #include #include using namespace std; void gotoxy(int x, int y) { HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); COORD cursorLoc; cout.flush(); cursorLoc.X = x; cursorLoc.Y = y; SetConsoleCursorPosition(hConsole, cursorLoc); } void setColor(int color, int bgcolor) { WORD Color; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); Color = (bgcolor<<4 & 0xF0) + (color & 0x0F); SetConsoleTextAttribute(hStdOut, Color); } void clrscr() { setColor(0, 0); system("cls"); // not recommender, but faster /* // compatible, but slower gotoxy(0, 0); for (size_t i = 0; i < 25; i++) cout << endl; */ } void zadanie1() { cout << "Zadanie 1" << endl; } void zadanie2() { cout << "Zadanie 2" << endl; } void zadanie3() { cout << "Zadanie 3" << endl; } void zadanie4() { cout << "Zadanie 4" << endl; } void zadanie(int n) { switch (n) { case 1: zadanie1(); break; case 2: zadanie2(); break; case 3: zadanie3(); break; case 4: zadanie4(); break; default: break; } } void drawArrow() { setColor(0, 10); cout << " <-"; setColor(10, 0); } void menu(int n /* n - menu item to highlight by arrow */) { clrscr(); setColor(10, 0); // set color of the text and background color gotoxy(10, 5); cout << "1. Input"; if (n == 1) drawArrow(); cout << endl; gotoxy(10, 6); cout << "2. Output"; if (n == 2) drawArrow(); cout << endl; gotoxy(10, 7); cout << "3. Search"; if (n == 3) drawArrow(); cout << endl; gotoxy(10, 8); cout << "4. Exit"; if (n == 4) drawArrow(); cout << endl; setColor(15, 0); cout << endl << "Use 'Up arrow' or 'Down arrow' to choose "; cout << endl << "Press ESC to EXIT..."; gotoxy(10, 20); cout << endl; } int main() { int menuItem = 1; char choiceChar = 0; menu(menuItem); while ((choiceChar = _getch()) != 27) // while not ESC { switch (choiceChar) { case 72: menuItem--; break; // UpArrow case 80: menuItem++; break; // DownArrow case 13: // Enter { zadanie(menuItem); _getch(); break; } default: cout << "Wrong choise!"; } if (menuItem < 1) menuItem = 4; // wrapping if (menuItem > 4) menuItem = 1; menu(menuItem); cout << endl << choiceChar << " (" << (int)choiceChar << ")"; // code of the key } return 0; }