[C/C++]使用上下左右控制菜单项

来源:互联网 发布:淘宝提升信誉的方法 编辑:程序博客网 时间:2024/05/17 15:35

实现功能:

使用上下键切换菜单项

使用左右键返回或进入

使用Esc键退出菜单


编译通过:VS2013


代码如下:


#include <iostream>  using std::cout;  using std::endl;  #include <string>  using std::string;  #include <conio.h>#include <windows.h>   #define MI 3//menu iterm number  void gotoxy(int x, int y){COORD coord;coord.X = x;coord.Y = y;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);}int main(void)  {            int fr = 0;//sign to row      int fc = 0;//sign to column      int g;//gets the keyboard value    string bars = "\n\n\t\t------------------------------------------------\n"  "\t\t|***--------------  Menu Bar  --------------***|\n"  "\t\t------------------------------------------------\n";    string menu[MI] = { "menu1", "menu2", "menu3" };      string pich = "\t\t\t\t    ->\t";  loop:system("cls");cout << bars;    do {gotoxy(0, 6);         for (int index = 0; index < MI; ++index)          {              if (fr == index)              {                  cout << pich;                  cout << menu[index] << endl;              }              else                  cout << "\t\t\t\t\t" << menu[index] << endl;              if (fc != 0)              {                  system("cls"); cout << bars;                if (fc == 1)                  {                      if (fr == 0)                          cout << "\t\t\t\tenter " << menu[0];                      else if (fr == 1)                          cout << "\t\t\t\tenter " << menu[1];                      else if (fr == 2)                          cout << "\t\t\t\tenter " << menu[2];                  }                  else if (fc == -1)                      cout << "\t\t\tReturn to the previous menu!";  cout << "\n\t\t\t\t";                system("pause");                  fc = 0;                  goto loop;              }          }          g = _getch();          switch (g)          {          case 0x48://up              --fr;              if (fr < 0)                  fr = MI - 1;              break;          case 0x50://down              ++fr;              if (fr > MI - 1)                  fr = 0;              break;          case 0x4b://left              fc = -1;              break;          case 0x4d://right              fc = 1;              break;          }      } while (g != 0x1b);//Esc to quit            return 0;  }  



0 0
原创粉丝点击