这是一个可以使用键盘的w(上)和s(下)来控制选择的光标上下移动的菜单,当然你也可以自定义其他按键进行控制。
这是我写的源代码:
#include <iostream>
#include <conio.h>
#include <Windows.h>
using namespace std;
void printMenu() {
system("cls");
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ 北京地铁线路小助手 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ [ ]查看所有站点 ]" << endl;
cout << "\t\t\t\t[ [ ]查看地铁线路 ]" << endl;
cout << "\t\t\t\t[ [ ]路线规划 ]" << endl;
cout << "\t\t\t\t[ [ ]价格计算 ]" << endl;
cout << "\t\t\t\t[ [ ]站点查询 ]" << endl;
cout << "\t\t\t\t[ [ ]打开线路图 ]" << endl;
cout << "\t\t\t\t[ [ ]系统维护 ]" << endl;
cout << "\t\t\t\t[ [ ]退出本系统 ]" << endl;
cout << "\t\t\t\t==========================" << endl;
cout << "\t\t\t\t[ Jerry Coding With Love ]" << endl;
cout << "\t\t\t\t==========================" << endl;
return;
}
void printSelection(short x, short y, bool isClear = false) {
CONSOLE_SCREEN_BUFFER_INFO cursorInfo;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hOut, &cursorInfo);
COORD bakPos = cursorInfo.dwCursorPosition;
COORD pos = { x,y };
SetConsoleCursorPosition(hOut, pos);
if (isClear) {
cout << " ";
}
else {
cout << "@";
}
SetConsoleCursorPosition(hOut, bakPos);
return;
}
int main() {
short defaultX = 39;
short defaultY = 3;
char inputChar;
int currChoice = 0;
printMenu();
printSelection(defaultX, defaultY);
while (1) {
inputChar = _getch();
if (inputChar == 13) {
// 循环条件:输入的字符不等于回车
break;
}
if (inputChar == 119 && currChoice > 0) {
// 箭头向上移动
printSelection(defaultX, defaultY, true);
printSelection(defaultX, --defaultY);
currChoice--;
}
else if (inputChar == 115 && currChoice < 7) {
// 箭头向下移动
printSelection(defaultX, defaultY, true);
printSelection(defaultX, ++defaultY);
currChoice++;
}
}
cout << "Your Choice:" << currChoice;
return 0;
}
© 版权声明
文章版权归作者所有,请勿转载至任何平台。
THE END
暂无评论内容