如何用c++写一个下拉菜单呢
时间: 2023-07-04 21:10:33 浏览: 70
下拉菜单可以通过在控制台或图形界面中绘制一个按钮和一个下拉框来实现。以下是在控制台中使用 C++ 编写一个简单的下拉菜单的示例代码:
```cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 定义菜单选项结构体
struct MenuItem {
string text; // 菜单项文本
int value; // 菜单项值
};
// 定义下拉菜单类
class DropdownMenu {
public:
DropdownMenu(int x, int y, int width, vector<MenuItem> items) {
// 初始化菜单属性
this->x = x;
this->y = y;
this->width = width;
this->items = items;
this->isExpanded = false;
this->selectedIndex = 0;
}
// 显示下拉菜单
void Show() {
// 显示按钮和当前选中菜单项文本
cout << string(x, ' ') << "+-------------------+" << endl;
cout << string(x, ' ') << "| " << items[selectedIndex].text;
cout << string(width - items[selectedIndex].text.size() - 3, ' ') << "v|" << endl;
cout << string(x, ' ') << "+-------------------+" << endl;
// 如果菜单已展开,则显示所有菜单项
if (isExpanded) {
for (int i = 0; i < items.size(); i++) {
cout << string(x, ' ') << "| " << items[i].text;
if (i == selectedIndex) {
cout << string(width - items[i].text.size() - 3, ' ') << "v|" << endl;
} else {
cout << string(width - items[i].text.size() - 3, ' ') << "|" << endl;
}
}
cout << string(x, ' ') << "+-------------------+" << endl;
}
}
// 处理用户输入
void HandleInput(char input) {
if (input == ' ') {
// 空格键展开/收起菜单
isExpanded = !isExpanded;
} else if (isExpanded && input == '\r') {
// 回车键选择当前选中菜单项
isExpanded = false;
} else if (isExpanded && input == 'w' && selectedIndex > 0) {
// w 键向上选择菜单项
selectedIndex--;
} else if (isExpanded && input == 's' && selectedIndex < items.size() - 1) {
// s 键向下选择菜单项
selectedIndex++;
}
}
// 获取当前选中菜单项的值
int GetSelectedValue() {
return items[selectedIndex].value;
}
private:
int x; // 菜单左上角 x 坐标
int y; // 菜单左上角 y 坐标
int width; // 菜单宽度
vector<MenuItem> items; // 菜单项列表
bool isExpanded; // 菜单是否展开
int selectedIndex; // 当前选中的菜单项索引
};
// 示例程序
int main() {
// 初始化菜单项列表
vector<MenuItem> items;
items.push_back({ "Option 1", 1 });
items.push_back({ "Option 2", 2 });
items.push_back({ "Option 3", 3 });
// 创建下拉菜单
DropdownMenu menu(10, 10, 10, items);
// 循环运行程序,直到用户输入 q 键退出
char input;
do {
// 显示下拉菜单
menu.Show();
// 处理用户输入
cin >> input;
menu.HandleInput(input);
} while (input != 'q');
// 输出选中的菜单项值
cout << "Selected value: " << menu.GetSelectedValue() << endl;
return 0;
}
```
该示例程序在控制台中创建了一个包含三个菜单项的下拉菜单,用户可以通过空格键展开/收起菜单、w/s 键选择菜单项、回车键选择当前选中菜单项,q 键退出程序。在退出程序时,程序会输出当前选中菜单项的值。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)