showcursor

本文详细介绍了Windows API中的ShowCursor函数,该函数通过调整内部计数器来实现光标的显示或隐藏。文章解释了函数参数bShow的作用及如何影响计数器,并说明了不同情况下光标显示的初始状态。
  name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-5572165936844014&dt=1193665761703&lmt=1193665780&format=336x280_as&output=html&correlator=1193665761687&url=http%3A%2F%2Fwww.codeguru.cn%2Fpublic%2Fiframe%2Fwinapiiframe.htm&color_bg=FFFFFF&color_text=000000&color_link=000000&color_url=FFFFFF&color_border=FFFFFF&ad_type=text&ga_vid=1285758818.1193665762&ga_sid=1193665762&ga_hid=111695597&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=8&u_java=true" frameborder="0" width="336" scrolling="no" height="280" allowtransparency="allowtransparency">     函数功能:该函数显示或隐藏光标。

    函数原型:int ShowCursor(BOOL bShow);

    参数:

    bShow:确定内部的显示计数器是增加还是减少,如果bShow为TRUE,则显示计数器增加1,如果bShow为FALSE,则计数器减1。

    返回值:返回值规定新的显示计数器。

    备注:该函数设置了一个内部显示计数器以确定光标是否显示,仅当显示计数器的值大于或等于0时,光标才显示,如果安装了鼠标,则显示计数的初始值为0。如果没有安装鼠标,显示计数是C1。

    速查:Windows NT:3.1及以上版本;Windows:95及以上版本;Windows CE:不支持;头文件:winuser.h;库文件:user32.lib。

#include <graphics.h> #include <conio.h> #include <stdio.h> #include <string.h> #include <windows.h> #include <tchar.h> #include <time.h> #include <vector> #include <algorithm> using namespace std; // ================== 公共定义 ================== enum LoginState { LOGGING_IN, LOGIN_SUCCESS, LOGIN_FAILED }; enum UserType { ORDINARY_USER, ADMIN_USER }; // ================== 用户信息结构 ================== struct UserInfo { TCHAR username[50]; UserType type; }; // ================== 图书信息结构 ================== struct Book { int id; TCHAR title[100]; TCHAR author[50]; TCHAR publisher[50]; int year; int quantity; Book* next; }; // ================== 全局变量 ================== // 图书管理相关 vector<Book*> bookArray; Book* bookList = nullptr; const int PAGE_SIZE = 5; int currentPage = 0; int totalPages = 0; int selectedBookId = -1; bool isEditing = false; // ================== 函数声明 ================== // 登录模块 UserInfo loginModule(); void drawLoginInterface(LoginState state, const TCHAR* username, const TCHAR* password, bool showPassword, bool isAdminLogin, bool usernameSelected, bool passwordSelected, bool showCursor); void showMessage(const TCHAR* message, bool isSuccess = true); void registerUser(const TCHAR* username, const TCHAR* password); // 图书管理模块 void initializeBooks(); void addBook(Book* newBook); void deleteBook(int id); void drawBookManagement(); void drawBookEdit(bool isAdding); void bookManagementSystem(); // ================== 主函数 ================== int main() { // 初始化图形界面 initgraph(700, 500); BeginBatchDraw(); // 1. 运行登录模块 UserInfo currentUser = loginModule(); // 2. 检查是否管理员登录成功 if (currentUser.type == ADMIN_USER && _tcscmp(currentUser.username, _T("admin")) == 0) { // 3. 进入图书管理模块 bookManagementSystem(); } else { // 普通用户或其他情况处理 MessageBox(NULL, _T("欢迎使用图书借阅系统"), _T("系统提示"), MB_OK | MB_ICONINFORMATION); } // 清理资源 EndBatchDraw(); closegraph(); return 0; } // ================== 登录模块实现 ================== UserInfo loginModule() { UserInfo user = { _T(""), ORDINARY_USER }; LoginState loginState = LOGGING_IN; TCHAR username[50] = _T(""); TCHAR password[50] = _T(""); bool usernameSelected = true; bool passwordSelected = false; bool showPassword = false; bool isAdminLogin = false; clock_t cursorTimer = clock(); bool showCursor = true; while (true) { // 处理光标闪烁 if (clock() - cursorTimer > 500) { showCursor = !showCursor; cursorTimer = clock(); } // 绘制界面 drawLoginInterface(loginState, username, password, showPassword, isAdminLogin, usernameSelected, passwordSelected, showCursor); // 处理鼠标事件 MOUSEMSG m; while (MouseHit()) { m = GetMouseMsg(); if (m.uMsg == WM_LBUTTONDOWN) { // 点击用户名输入框 if (m.x > 270 && m.x < 510 && m.y > 175 && m.y < 215) { usernameSelected = true; passwordSelected = false; } // 点击密码输入框 else if (m.x > 270 && m.x < 540 && m.y > 235 && m.y < 275) { usernameSelected = false; passwordSelected = true; } // 点击显示/隐藏密码按钮 else if (m.x > 515 && m.x < 540 && m.y > 235 && m.y < 275) { showPassword = !showPassword; } // 点击管理员登录选项 else if (m.x > 270 && m.x < 290 && m.y > 285 && m.y < 305) { isAdminLogin = !isAdminLogin; } // 点击登录按钮 else if (m.x > 250 && m.x < 450 && m.y > 330 && m.y < 380) { if (_tcscmp(username, _T("")) == 0 || _tcscmp(password, _T("")) == 0) { showMessage(_T("用户名和密码不能为空"), false); } else if (isAdminLogin) { if (_tcscmp(username, _T("admin")) == 0 && _tcscmp(password, _T("admin123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("管理员登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回管理员信息 _tcscpy_s(user.username, username); user.type = ADMIN_USER; return user; } else { showMessage(_T("管理员账号或密码错误"), false); } } else { if (_tcscmp(username, _T("user")) == 0 && _tcscmp(password, _T("user123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回普通用户信息 _tcscpy_s(user.username, username); user.type = ORDINARY_USER; return user; } else { showMessage(_T("用户名或密码错误"), false); } } _tcscpy_s(password, _T("")); } // 点击注册按钮 else if (m.x > 250 && m.x < 450 && m.y > 390 && m.y < 440) { if (_tcscmp(username, _T("")) == 0 || _tcscmp(password, _T("")) == 0) { showMessage(_T("用户名和密码不能为空"), false); } else { registerUser(username, password); _tcscpy_s(username, _T("")); _tcscpy_s(password, _T("")); } } } } // 处理键盘事件 while (_kbhit()) { int key = _getch(); if (key == 8) { // 退格键 if (usernameSelected && _tcslen(username) > 0) { username[_tcslen(username) - 1] = _T('\0'); } else if (passwordSelected && _tcslen(password) > 0) { password[_tcslen(password) - 1] = _T('\0'); } } else if (key == 9) { // Tab键 if (usernameSelected) { usernameSelected = false; passwordSelected = true; } else { usernameSelected = true; passwordSelected = false; } } else if (key == 13) { // 回车键 if (_tcscmp(username, _T("")) == 0 || _tcscmp(password, _T("")) == 0) { showMessage(_T("用户名和密码不能为空"), false); } else if (isAdminLogin) { if (_tcscmp(username, _T("admin")) == 0 && _tcscmp(password, _T("admin123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("管理员登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回管理员信息 _tcscpy_s(user.username, username); user.type = ADMIN_USER; return user; } else { showMessage(_T("管理员账号或密码错误"), false); } } else { if (_tcscmp(username, _T("user")) == 0 && _tcscmp(password, _T("user123")) == 0) { loginState = LOGIN_SUCCESS; showMessage(_T("登录成功!")); FlushBatchDraw(); Sleep(1000); // 返回普通用户信息 _tcscpy_s(user.username, username); user.type = ORDINARY_USER; return user; } else { showMessage(_T("用户名或密码错误"), false); } } _tcscpy_s(password, _T("")); } else if (key >= 32 && key <= 126) { // 可打印字符 TCHAR c = (TCHAR)key; size_t len; if (usernameSelected) { len = _tcslen(username); if (len < 49) { username[len] = c; username[len + 1] = _T('\0'); } } else if (passwordSelected) { len = _tcslen(password); if (len < 49) { password[len] = c; password[len + 1] = _T('\0'); } } } } FlushBatchDraw(); Sleep(20); } return user; // 正常情况下不会执行到这里 } void drawLoginInterface(LoginState state, const TCHAR* username, const TCHAR* password, bool showPassword, bool isAdminLogin, bool usernameSelected, bool passwordSelected, bool showCursor) { // 设置背景 setbkcolor(RGB(240, 245, 255)); cleardevice(); // 绘制标题 settextcolor(RGB(25, 118, 210)); settextstyle(36, 0, _T("宋体")); outtextxy(220, 50, _T("图书借阅系统")); // 绘制系统信息 settextcolor(RGB(100, 100, 100)); settextstyle(14, 0, _T("宋体")); outtextxy(300, 100, _T("v1.2")); // 绘制登录框 setfillcolor(RGB(255, 255, 255)); setlinecolor(RGB(200, 200, 200)); fillrectangle(150, 150, 550, 450); // 绘制用户名标签 settextcolor(RGB(80, 80, 80)); settextstyle(20, 0, _T("宋体")); outtextxy(180, 180, _T("用户名:")); // 绘制用户名输入框 setfillcolor(usernameSelected ? RGB(235, 245, 255) : RGB(245, 245, 245)); fillrectangle(270, 175, 510, 215); setlinecolor(usernameSelected ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(270, 175, 510, 215); // 绘制用户名文本 settextcolor(RGB(50, 50, 50)); settextstyle(18, 0, _T("宋体")); outtextxy(275, 180, username); // 绘制光标 if (usernameSelected && showCursor) { int textWidth = textwidth(username); line(275 + textWidth, 180, 275 + textWidth, 205); } // 绘制密码标签 settextcolor(RGB(80, 80, 80)); settextstyle(20, 0, _T("宋体")); outtextxy(180, 240, _T("密 码:")); // 绘制密码输入框 setfillcolor(passwordSelected ? RGB(235, 245, 255) : RGB(245, 245, 245)); fillrectangle(270, 235, 510, 275); setlinecolor(passwordSelected ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(270, 235, 510, 275); // 创建掩码密码字符串 TCHAR maskedPassword[50] = _T(""); if (!showPassword) { size_t len = _tcslen(password); for (size_t i = 0; i < len; i++) { _tcscat_s(maskedPassword, 50, _T("?")); } } // 绘制密码文本 settextcolor(RGB(50, 50, 50)); settextstyle(18, 0, _T("宋体")); if (showPassword) { outtextxy(275, 240, password); } else { outtextxy(275, 240, maskedPassword); } // 绘制光标 if (passwordSelected && showCursor) { const TCHAR* displayText = showPassword ? password : maskedPassword; int textWidth = textwidth(displayText); line(275 + textWidth, 240, 275 + textWidth, 265); } // 显示/隐藏密码按钮 setfillcolor(showPassword ? RGB(220, 237, 255) : RGB(245, 245, 245)); fillrectangle(515, 235, 540, 275); setlinecolor(RGB(180, 180, 180)); rectangle(515, 235, 540, 275); settextcolor(RGB(100, 100, 100)); settextstyle(16, 0, _T("宋体")); outtextxy(520, 240, showPassword ? _T("??") : _T("??")); // 管理员登录选项 setfillcolor(RGB(245, 245, 245)); fillrectangle(270, 285, 290, 305); setlinecolor(RGB(180, 180, 180)); rectangle(270, 285, 290, 305); if (isAdminLogin) { setlinecolor(RGB(25, 118, 210)); setlinestyle(PS_SOLID, 2); line(272, 292, 282, 302); line(282, 302, 288, 286); setlinestyle(PS_SOLID, 1); } settextcolor(RGB(80, 80, 80)); settextstyle(16, 0, _T("宋体")); outtextxy(300, 285, _T("管理员登录")); // 绘制登录按钮 setfillcolor(RGB(245, 245, 245)); fillroundrect(250, 330, 450, 380, 10, 10); settextcolor(RGB(25, 118, 210)); settextstyle(24, 0, _T("宋体")); outtextxy(320, 340, _T("登 录")); // 绘制注册按钮 setfillcolor(RGB(245, 245, 245)); fillroundrect(250, 390, 450, 440, 10, 10); settextcolor(RGB(25, 118, 210)); settextstyle(24, 0, _T("宋体")); outtextxy(320, 400, _T("注 册")); // 绘制状态信息 if (state == LOGIN_FAILED) { settextcolor(RGB(211, 47, 47)); settextstyle(16, 0, _T("宋体")); outtextxy(250, 305, _T("用户名或密码错误,请重试")); } else if (state == LOGIN_SUCCESS) { settextcolor(RGB(56, 142, 60)); settextstyle(16, 0, _T("宋体")); outtextxy(300, 305, _T("登录成功!")); } // 绘制版权信息 settextcolor(RGB(150, 150, 150)); settextstyle(12, 0, _T("宋体")); outtextxy(240, 480, _T("? 2023 计算机科学与技术2304114班")); } void showMessage(const TCHAR* message, bool isSuccess) { settextcolor(isSuccess ? RGB(56, 142, 60) : RGB(211, 47, 47)); settextstyle(16, 0, _T("宋体")); outtextxy(250, 305, message); FlushBatchDraw(); Sleep(2000); } void registerUser(const TCHAR* username, const TCHAR* password) { // 在实际应用中,这里应该将用户信息保存到数据库或文件 showMessage(_T("注册成功! 请使用新账号登录"), true); } // ================== 图书管理模块实现 ================== void initializeBooks() { Book* books[] = { new Book{1, _T("C++程序设计"), _T("谭浩强"), _T("清华大学出版社"), 2019, 10, nullptr}, new Book{2, _T("数据结构"), _T("严蔚敏"), _T("清华大学出版社"), 2020, 8, nullptr}, new Book{3, _T("算法导论"), _T("Thomas Cormen"), _T("MIT Press"), 2009, 5, nullptr}, new Book{4, _T("深入理解计算机系统"), _T("Bryant"), _T("机械工业出版社"), 2016, 7, nullptr}, new Book{5, _T("设计模式"), _T("Erich Gamma"), _T("机械工业出版社"), 2020, 6, nullptr} }; // 构建链表 for (int i = 0; i < sizeof(books) / sizeof(books[0]); i++) { books[i]->next = bookList; bookList = books[i]; bookArray.push_back(books[i]); } // 按ID排序数组 sort(bookArray.begin(), bookArray.end(), [](Book* a, Book* b) { return a->id < b->id; }); totalPages = (bookArray.size() + PAGE_SIZE - 1) / PAGE_SIZE; } // 添加新图书 void addBook(Book* newBook) { // 添加到链表头部 newBook->next = bookList; bookList = newBook; // 添加到数组并排序 bookArray.push_back(newBook); sort(bookArray.begin(), bookArray.end(), [](Book* a, Book* b) { return a->id < b->id; }); totalPages = (bookArray.size() + PAGE_SIZE - 1) / PAGE_SIZE; } // 删除图书 void deleteBook(int id) { // 从链表中删除 Book* prev = nullptr; Book* current = bookList; while (current != nullptr) { if (current->id == id) { if (prev) { prev->next = current->next; } else { bookList = current->next; } break; } prev = current; current = current->next; } // 从数组中删除 for (auto it = bookArray.begin(); it != bookArray.end(); ) { if ((*it)->id == id) { delete* it; // 释放内存 it = bookArray.erase(it); } else { ++it; } } totalPages = (bookArray.size() + PAGE_SIZE - 1) / PAGE_SIZE; if (currentPage >= totalPages && totalPages > 0) { currentPage = totalPages - 1; } } // 绘制图书管理界面 void drawBookManagement() { // 设置背景 setbkcolor(RGB(240, 245, 255)); cleardevice(); // 绘制标题 settextcolor(RGB(25, 118, 210)); settextstyle(36, 0, _T("宋体")); outtextxy(250, 20, _T("图书管理系统")); // 绘制功能按钮 setfillcolor(RGB(25, 118, 210)); settextcolor(WHITE); settextstyle(20, 0, _T("宋体")); // 添加图书按钮 fillroundrect(50, 80, 180, 120, 10, 10); outtextxy(70, 85, _T("添加图书")); // 删除图书按钮 fillroundrect(210, 80, 340, 120, 10, 10); outtextxy(230, 85, _T("删除图书")); // 退出系统按钮 fillroundrect(530, 80, 660, 120, 10, 10); outtextxy(550, 85, _T("退出系统")); // 绘制图书列表标题 setfillcolor(RGB(200, 220, 255)); fillrectangle(50, 140, 650, 170); settextcolor(RGB(25, 118, 210)); settextstyle(18, 0, _T("宋体")); outtextxy(60, 145, _T("ID")); outtextxy(120, 145, _T("书名")); outtextxy(330, 145, _T("作者")); outtextxy(450, 145, _T("出版社")); outtextxy(560, 145, _T("年份")); outtextxy(620, 145, _T("库存")); // 绘制图书列表 int startIdx = currentPage * PAGE_SIZE; int endIdx = min(startIdx + PAGE_SIZE, (int)bookArray.size()); int y = 180; for (int i = startIdx; i < endIdx; i++) { Book* book = bookArray[i]; // 设置选中背景 if (book->id == selectedBookId) { setfillcolor(RGB(230, 240, 255)); solidrectangle(50, y, 650, y + 30); } else { setfillcolor(i % 2 == 0 ? RGB(245, 250, 255) : RGB(255, 255, 255)); solidrectangle(50, y, 650, y + 30); } setlinecolor(RGB(220, 220, 220)); rectangle(50, y, 650, y + 30); // 绘制图书信息 settextcolor(RGB(50, 50, 50)); TCHAR buffer[50]; _stprintf_s(buffer, _T("%d"), book->id); outtextxy(60, y + 5, buffer); outtextxy(120, y + 5, book->title); outtextxy(330, y + 5, book->author); outtextxy(450, y + 5, book->publisher); _stprintf_s(buffer, _T("%d"), book->year); outtextxy(560, y + 5, buffer); _stprintf_s(buffer, _T("%d"), book->quantity); outtextxy(620, y + 5, buffer); y += 32; } // 绘制分页控件 setfillcolor(RGB(245, 245, 245)); fillroundrect(300, 450, 380, 490, 10, 10); fillroundrect(400, 450, 480, 490, 10, 10); settextcolor(RGB(25, 118, 210)); TCHAR pageBuffer[20]; _stprintf_s(pageBuffer, _T("%d/%d"), currentPage + 1, totalPages); outtextxy(385, 455, pageBuffer); settextstyle(20, 0, _T("宋体")); outtextxy(320, 450, _T("<")); outtextxy(440, 450, _T(">")); // 绘制编辑按钮 if (selectedBookId != -1) { setfillcolor(RGB(25, 118, 210)); fillroundrect(500, 450, 620, 490, 10, 10); settextcolor(WHITE); outtextxy(520, 455, _T("编辑图书")); } } // 绘制添加/编辑图书界面 void drawBookEdit(bool isAdding) { static int lastEditedId = -1; static bool firstEntry = true; static TCHAR title[100] = _T(""); static TCHAR author[50] = _T(""); static TCHAR publisher[50] = _T(""); static TCHAR yearStr[10] = _T(""); static TCHAR quantityStr[10] = _T(""); static int fieldSelected = 0; // 0:书名, 1:作者, 2:出版社, 3:年份, 4:库存 static clock_t cursorTimer = clock(); static bool showCursor = true; // 检查是否需要重置编辑数据 if (firstEntry) { if (isAdding) { _tcscpy_s(title, _T("")); _tcscpy_s(author, _T("")); _tcscpy_s(publisher, _T("")); _tcscpy_s(yearStr, _T("")); _tcscpy_s(quantityStr, _T("")); } else { // 从当前选中书籍加载数据 for (Book* book : bookArray) { if (book->id == selectedBookId) { _tcscpy_s(title, book->title); _tcscpy_s(author, book->author); _tcscpy_s(publisher, book->publisher); _stprintf_s(yearStr, _T("%d"), book->year); _stprintf_s(quantityStr, _T("%d"), book->quantity); break; } } } firstEntry = false; lastEditedId = isAdding ? -1 : selectedBookId; } // 光标闪烁处理 if (clock() - cursorTimer > 500) { showCursor = !showCursor; cursorTimer = clock(); } // 设置背景 setbkcolor(RGB(240, 245, 255)); cleardevice(); // 绘制标题 settextcolor(RGB(25, 118, 210)); settextstyle(36, 0, _T("宋体")); outtextxy(250, 30, isAdding ? _T("添加图书") : _T("编辑图书")); // 绘制表单 int y = 100; const int fieldHeight = 40; const int buttonY = 360; // 固定按钮位置 // 书名 settextcolor(RGB(80, 80, 80)); settextstyle(20, 0, _T("宋体")); outtextxy(150, y + 10, _T("书名:")); setfillcolor(fieldSelected == 0 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 0 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, title); if (fieldSelected == 0 && showCursor) { int textWidth = textwidth(title); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 作者 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("作者:")); setfillcolor(fieldSelected == 1 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 1 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, author); if (fieldSelected == 1 && showCursor) { int textWidth = textwidth(author); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 出版社 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("出版社:")); setfillcolor(fieldSelected == 2 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 2 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, publisher); if (fieldSelected == 2 && showCursor) { int textWidth = textwidth(publisher); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 出版年份 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("年份:")); setfillcolor(fieldSelected == 3 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 3 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, yearStr); if (fieldSelected == 3 && showCursor) { int textWidth = textwidth(yearStr); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } y += fieldHeight + 10; // 库存数量 settextcolor(RGB(80, 80, 80)); outtextxy(150, y + 10, _T("库存:")); setfillcolor(fieldSelected == 4 ? RGB(235, 245, 255) : RGB(245, 245, 245)); solidrectangle(250, y, 550, y + fieldHeight); setlinecolor(fieldSelected == 4 ? RGB(25, 118, 210) : RGB(180, 180, 180)); rectangle(250, y, 550, y + fieldHeight); settextcolor(RGB(50, 50, 50)); outtextxy(260, y + 10, quantityStr); if (fieldSelected == 4 && showCursor) { int textWidth = textwidth(quantityStr); line(260 + textWidth, y + 10, 260 + textWidth, y + fieldHeight - 10); } // 保存按钮 setfillcolor(RGB(25, 118, 210)); fillroundrect(250, buttonY, 380, buttonY + 50, 10, 10); settextcolor(WHITE); settextstyle(20, 0, _T("宋体")); outtextxy(280, buttonY + 15, _T("保存")); // 取消按钮 setfillcolor(RGB(200, 200, 200)); fillroundrect(420, buttonY, 550, buttonY + 50, 10, 10); settextcolor(WHITE); outtextxy(450, buttonY + 15, _T("取消")); // 处理鼠标事件 MOUSEMSG m; while (MouseHit()) { m = GetMouseMsg(); if (m.uMsg == WM_LBUTTONDOWN) { // 点击输入框 if (m.x > 250 && m.x < 550) { if (m.y > 100 && m.y < 140) fieldSelected = 0; else if (m.y > 150 && m.y < 190) fieldSelected = 1; else if (m.y > 200 && m.y < 240) fieldSelected = 2; else if (m.y > 250 && m.y < 290) fieldSelected = 3; else if (m.y > 300 && m.y < 340) fieldSelected = 4; } // 点击保存按钮 else if (m.x >= 250 && m.x <= 380 && m.y >= buttonY && m.y <= buttonY + 50) { setfillcolor(RGB(15, 98, 200)); fillroundrect(250, buttonY, 380, buttonY + 50, 10, 10); outtextxy(280, buttonY + 15, _T("保存")); FlushBatchDraw(); Sleep(100); int year = _ttoi(yearStr); int quantity = _ttoi(quantityStr); if (_tcslen(title) == 0) { MessageBox(NULL, _T("书名不能为空"), _T("错误"), MB_OK | MB_ICONERROR); } else if (year < 1900 || year > 2100) { MessageBox(NULL, _T("出版年份必须在1900-2100之间"), _T("错误"), MB_OK | MB_ICONERROR); } else if (quantity <= 0) { MessageBox(NULL, _T("库存数量必须大于0"), _T("错误"), MB_OK | MB_ICONERROR); } else { if (isAdding) { // 生成新ID int newId = 1; if (!bookArray.empty()) { newId = bookArray.back()->id + 1; } Book* newBook = new Book{ newId, _T(""), _T(""), _T(""), year, quantity, nullptr }; _tcscpy_s(newBook->title, title); _tcscpy_s(newBook->author, author); _tcscpy_s(newBook->publisher, publisher); addBook(newBook); MessageBox(NULL, _T("图书添加成功"), _T("成功"), MB_OK | MB_ICONINFORMATION); } else { // 更新现有图书 for (Book* book : bookArray) { if (book->id == selectedBookId) { _tcscpy_s(book->title, title); _tcscpy_s(book->author, author); _tcscpy_s(book->publisher, publisher); book->year = year; book->quantity = quantity; break; } } MessageBox(NULL, _T("图书信息更新成功"), _T("成功"), MB_OK | MB_ICONINFORMATION); } isEditing = false; firstEntry = true; // 重置标志 return; } } // 点击取消按钮 else if (m.x >= 420 && m.x <= 550 && m.y >= buttonY && m.y <= buttonY + 50) { // 添加点击反馈 setfillcolor(RGB(180, 180, 180)); fillroundrect(420, buttonY, 550, buttonY + 50, 10, 10); outtextxy(450, buttonY + 15, _T("取消")); FlushBatchDraw(); Sleep(100); isEditing = false; firstEntry = true; return; } } } // 处理键盘输入 while (_kbhit()) { wint_t key = _getwch(); if (key == 8) { // 退格键 if (fieldSelected == 0 && _tcslen(title) > 0) { title[_tcslen(title) - 1] = _T('\0'); } else if (fieldSelected == 1 && _tcslen(author) > 0) { author[_tcslen(author) - 1] = _T('\0'); } else if (fieldSelected == 2 && _tcslen(publisher) > 0) { publisher[_tcslen(publisher) - 1] = _T('\0'); } else if (fieldSelected == 3 && _tcslen(yearStr) > 0) { yearStr[_tcslen(yearStr) - 1] = _T('\0'); } else if (fieldSelected == 4 && _tcslen(quantityStr) > 0) { quantityStr[_tcslen(quantityStr) - 1] = _T('\0'); } } else if (key == 9 || key == 13) { // Tab键或回车键切换字段 fieldSelected = (fieldSelected + 1) % 5; } else if (key >= 32) { // 可打印字符 TCHAR c = (TCHAR)key; if (fieldSelected == 0 && _tcslen(title) < 99) { size_t len = _tcslen(title); title[len] = c; title[len + 1] = _T('\0'); } else if (fieldSelected == 1 && _tcslen(author) < 49) { size_t len = _tcslen(author); author[len] = c; author[len + 1] = _T('\0'); } else if (fieldSelected == 2 && _tcslen(publisher) < 49) { size_t len = _tcslen(publisher); publisher[len] = c; publisher[len + 1] = _T('\0'); } else if (fieldSelected == 3 && _tcslen(yearStr) < 4) { if (c >= '0' && c <= '9') { size_t len = _tcslen(yearStr); yearStr[len] = c; yearStr[len + 1] = _T('\0'); } } else if (fieldSelected == 4 && _tcslen(quantityStr) < 5) { if (c >= '0' && c <= '9') { size_t len = _tcslen(quantityStr); quantityStr[len] = c; quantityStr[len + 1] = _T('\0'); } } } } FlushBatchDraw(); Sleep(20); } // 图书管理主函数 void bookManagementSystem() { initializeBooks(); while (true) { if (isEditing) { drawBookEdit(false); } else { drawBookManagement(); // 只在非编辑状态下处理主界面事件 if (MouseHit()) { MOUSEMSG m = GetMouseMsg(); if (m.uMsg == WM_LBUTTONDOWN) { // 点击添加图书按钮 if (m.x > 50 && m.x < 180 && m.y > 80 && m.y < 120) { isEditing = true; // 清空鼠标消息队列 while (MouseHit()) GetMouseMsg(); } // 点击删除图书按钮 else if (m.x > 210 && m.x < 340 && m.y > 80 && m.y < 120 && selectedBookId != -1) { if (MessageBox(NULL, _T("确定要删除这本图书吗?"), _T("确认删除"), MB_YESNO | MB_ICONQUESTION) == IDYES) { deleteBook(selectedBookId); selectedBookId = -1; } } // 点击退出系统按钮 else if (m.x > 530 && m.x < 660 && m.y > 80 && m.y < 120) { if (MessageBox(NULL, _T("确定要退出系统吗?"), _T("确认退出"), MB_YESNO | MB_ICONQUESTION) == IDYES) { break; } } // 点击上一页按钮 else if (m.x > 300 && m.x < 380 && m.y > 450 && m.y < 490 && currentPage > 0) { currentPage--; } // 点击下一页按钮 else if (m.x > 400 && m.x < 480 && m.y > 450 && m.y < 490 && currentPage < totalPages - 1) { currentPage++; } // 点击编辑图书按钮 else if (m.x > 500 && m.x < 620 && m.y > 450 && m.y < 490 && selectedBookId != -1) { isEditing = true; // 清空鼠标消息队列 while (MouseHit()) GetMouseMsg(); } // 点击图书列表 else if (m.x > 50 && m.x < 650 && m.y > 180 && m.y < 180 + PAGE_SIZE * 32) { int idx = (m.y - 180) / 32; int bookIdx = currentPage * PAGE_SIZE + idx; if (bookIdx < bookArray.size()) { selectedBookId = bookArray[bookIdx]->id; } } } } } FlushBatchDraw(); Sleep(20); } 添加图书和编辑图书功能有bug,无法保存和取消(鼠标点击按钮没有反应),请你检查鼠标处理相关的代码发现问题所在并尝试解决。
07-02
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <conio.h> #include <windows.h> // 游戏常量 #define WIDTH 50 #define HEIGHT 20 #define MAX_ENEMIES 5 #define MAX_TREASURES 10 // 游戏元素 #define WALL '#' #define PATH ' ' #define PLAYER 'P' #define ENEMY 'E' #define TREASURE 'T' #define EXIT 'X' // 方向控制 #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 // 游戏状态 typedef enum { MAIN_MENU, PLAYING, GAME_OVER, LEVEL_COMPLETE, INSTRUCTIONS, ABOUT } GameState; // 游戏结构体 typedef struct { int x, y; } Position; // 全局变量 int gameRunning = 1; int gameOver = 0; int gameWin = 0; int level = 1; int score = 0; int lives = 3; int treasuresCollected = 0; int lastPlayerX = -1, lastPlayerY = -1; // 记录玩家上一次的位置 GameState gameState = MAIN_MENU; char maze[HEIGHT][WIDTH]; Position player; Position enemies[MAX_ENEMIES]; Position treasures[MAX_TREASURES]; Position exitPos; // 函数声明 void initGame(); void generateMaze(); void drawMainMenu(); void drawInstructions(); void drawAbout(); void drawGame(); void drawGameOver(); void drawLevelComplete(); void moveEnemies(); void processInput(); void setCursorPosition(int x, int y); void hideCursor(); void showCursor(); void drawBorder(); void drawTitle(); // 初始化游戏 void initGame() { // 重置游戏状态 gameOver = 0; gameWin = 0; treasuresCollected = 0; lastPlayerX = -1; lastPlayerY = -1; // 初始化迷宫(全部设为墙) for (int y = 0; y < HEIGHT; y++) { for (int x = 0; x < WIDTH; x++) { maze[y][x] = WALL; } } // 随机生成迷宫(深度优先算法) srand(time(NULL) + level); // 创建起点 int startX = 1, startY = 1; player.x = startX; player.y = startY; maze[startY][startX] = PATH; // 创建路径 int stack[WIDTH * HEIGHT][2]; int stackSize = 0; stack[stackSize][0] = startX; stack[stackSize][1] = startY; stackSize++; while (stackSize > 0) { int currentX = stack[stackSize-1][0]; int currentY = stack[stackSize-1][1]; // 查找可用方向 int directions[4][2] = {{2,0}, {-2,0}, {0,2}, {0,-2}}; int availableDirs[4] = {0}; int availableCount = 0; for (int i = 0; i < 4; i++) { int nextX = currentX + directions[i][0]; int nextY = currentY + directions[i][1]; if (nextX > 0 && nextX < WIDTH-1 && nextY > 0 && nextY < HEIGHT-1 && maze[nextY][nextX] == WALL) { availableDirs[availableCount] = i; availableCount++; } } if (availableCount > 0) { int dirIndex = availableDirs[rand() % availableCount]; int nextX = currentX + directions[dirIndex][0]; int nextY = currentY + directions[dirIndex][1]; // 打通墙壁 maze[nextY][nextX] = PATH; maze[currentY + directions[dirIndex][1]/2][currentX + directions[dirIndex][0]/2] = PATH; // 添加到栈 stack[stackSize][0] = nextX; stack[stackSize][1] = nextY; stackSize++; } else { // 回溯 stackSize--; } } // 创建出口(右下角) exitPos.x = WIDTH - 2; exitPos.y = HEIGHT - 2; maze[exitPos.y][exitPos.x] = EXIT; // 生成宝藏 int treasureCount = MAX_TREASURES - level; // 随关卡减少宝藏数量 if (treasureCount < 3) treasureCount = 3; for (int i = 0; i < treasureCount; i++) { int x, y; do { x = 1 + rand() % (WIDTH - 2); y = 1 + rand() % (HEIGHT - 2); } while (maze[y][x] != PATH || (x == player.x && y == player.y)); treasures[i].x = x; treasures[i].y = y; maze[y][x] = TREASURE; } // 生成敌人 int enemyCount = level < MAX_ENEMIES ? level : MAX_ENEMIES; for (int i = 0; i < enemyCount; i++) { int x, y; do { x = 1 + rand() % (WIDTH - 2); y = 1 + rand() % (HEIGHT - 2); } while (maze[y][x] != PATH || (x == player.x && y == player.y)); enemies[i].x = x; enemies[i].y = y; maze[y][x] = ENEMY; } } // 设置光标位置 void setCursorPosition(int x, int y) { COORD coord = {x, y}; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } // 隐藏光标 void hideCursor() { CONSOLE_CURSOR_INFO cursorInfo; cursorInfo.dwSize = 1; cursorInfo.bVisible = FALSE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo); } // 显示光标 void showCursor() { CONSOLE_CURSOR_INFO cursorInfo; cursorInfo.dwSize = 1; cursorInfo.bVisible = TRUE; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursorInfo); } // 绘制边框 void drawBorder() { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11); // 青色 for (int i = 0; i < WIDTH + 4; i++) { setCursorPosition(i, 0); printf("%c", 219); setCursorPosition(i, HEIGHT + 5); printf("%c", 219); } for (int i = 1; i < HEIGHT + 5; i++) { setCursorPosition(0, i); printf("%c", 219); setCursorPosition(WIDTH + 3, i); printf("%c", 219); } SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 } // 绘制标题 void drawTitle() { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 setCursorPosition(15, 2); printf("================================="); setCursorPosition(15, 3); printf("| 迷宫探险游戏 v1.0 |"); setCursorPosition(15, 4); printf("================================="); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 } // 绘制主菜单 void drawMainMenu() { system("cls"); drawBorder(); drawTitle(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); // 绿色 setCursorPosition(25, 8); printf("1. 开始游戏"); setCursorPosition(25, 10); printf("2. 游戏说明"); setCursorPosition(25, 12); printf("3. 关于游戏"); setCursorPosition(25, 14); printf("4. 退出游戏"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 setCursorPosition(15, 18); printf("使用方向键选择,按Enter确认"); setCursorPosition(20, 20); printf("当前等级: %d 最高分: %d", level, score); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 } // 绘制游戏说明 void drawInstructions() { system("cls"); drawBorder(); drawTitle(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11); // 青色 setCursorPosition(20, 7); printf("============ 游戏说明 ============"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 setCursorPosition(15, 9); printf("目标: 收集所有宝藏并找到出口"); setCursorPosition(15, 11); printf("控制: 方向键移动角色"); setCursorPosition(15, 13); printf("规则:"); setCursorPosition(17, 14); printf("- 收集黄色宝藏(T)增加分数"); setCursorPosition(17, 15); printf("- 避开红色敌人(E),否则会失去生命"); setCursorPosition(17, 16); printf("- 收集所有宝藏后才能通过绿色出口(X)"); setCursorPosition(17, 17); printf("- 每通过一关,难度会增加"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 setCursorPosition(20, 20); printf("按任意键返回主菜单..."); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 } // 绘制关于信息 void drawAbout() { system("cls"); drawBorder(); drawTitle(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11); // 青色 setCursorPosition(20, 7); printf("============ 关于游戏 ============"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 setCursorPosition(18, 9); printf("游戏名称: 迷宫探险"); setCursorPosition(18, 11); printf("版本: 1.0"); setCursorPosition(18, 13); printf("开发者: 学生项目"); setCursorPosition(18, 15); printf("描述: 一个使用C语言开发的迷宫探险游戏,"); setCursorPosition(18, 16); printf(" 包含随机生成的迷宫、敌人和宝藏。"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 setCursorPosition(20, 20); printf("按任意键返回主菜单..."); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 } // 绘制游戏界面(优化版,避免频闪) void drawGame() { // 首次绘制整个迷宫 static int firstDraw = 1; if (firstDraw) { system("cls"); // 清屏 drawBorder(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 setCursorPosition(WIDTH/2 - 10, 1); printf("===== 迷宫探险 - 第 %d 关 =====", level); for (int y = 0; y < HEIGHT; y++) { setCursorPosition(2, y + 3); for (int x = 0; x < WIDTH; x++) { if (x == player.x && y == player.y) { SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 printf("%c", PLAYER); } else { switch (maze[y][x]) { case WALL: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8); // 灰色 printf("%c", WALL); break; case PATH: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 printf("%c", PATH); break; case ENEMY: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // 红色 printf("%c", ENEMY); break; case TREASURE: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 printf("%c", TREASURE); break; case EXIT: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); // 绿色 printf("%c", EXIT); break; default: printf("%c", maze[y][x]); } } } printf("\n"); } // 重置控制台颜色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 显示游戏信息 setCursorPosition(2, HEIGHT + 4); printf("控制: 方向键移动 R: 重新开始 Q: 返回菜单"); setCursorPosition(2, HEIGHT + 5); printf("生命: "); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // 红色 for (int i = 0; i < lives; i++) printf("%c ", 3); // 心形符号 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); setCursorPosition(20, HEIGHT + 5); printf("分数: %d", score); setCursorPosition(40, HEIGHT + 5); printf("宝藏: %d/%d", treasuresCollected, MAX_TREASURES - level); firstDraw = 0; } else { // 更新玩家位置 if (lastPlayerX != -1 && lastPlayerY != -1) { // 恢复玩家上一次位置的内容 setCursorPosition(lastPlayerX + 2, lastPlayerY + 3); switch (maze[lastPlayerY][lastPlayerX]) { case WALL: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8); printf("%c", WALL); break; case PATH: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); printf("%c", PATH); break; case TREASURE: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); printf("%c", TREASURE); break; case EXIT: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); printf("%c", EXIT); break; case ENEMY: SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); printf("%c", ENEMY); break; } } // 绘制玩家当前位置 setCursorPosition(player.x + 2, player.y + 3); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 printf("%c", PLAYER); // 更新状态信息 setCursorPosition(2, HEIGHT + 5); printf("生命: "); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // 红色 for (int i = 0; i < lives; i++) printf("%c ", 3); // 心形符号 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); setCursorPosition(20, HEIGHT + 5); printf("分数: %d", score); setCursorPosition(40, HEIGHT + 5); printf("宝藏: %d/%d", treasuresCollected, MAX_TREASURES - level); } // 保存当前玩家位置 lastPlayerX = player.x; lastPlayerY = player.y; // 重置控制台颜色 SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); } // 绘制游戏结束界面 void drawGameOver() { system("cls"); drawBorder(); drawTitle(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); // 红色 setCursorPosition(25, 10); printf("======== 游戏结束 ========"); setCursorPosition(27, 12); printf("最终分数: %d", score); setCursorPosition(25, 14); printf("通关等级: %d", level - 1); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 setCursorPosition(22, 18); printf("按 R 重新开始游戏"); setCursorPosition(22, 20); printf("按 Q 返回主菜单"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 } // 绘制关卡完成界面 void drawLevelComplete() { system("cls"); drawBorder(); drawTitle(); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 10); // 绿色 setCursorPosition(25, 10); printf("===== 第 %d 关完成! =====", level); setCursorPosition(25, 12); printf("当前分数: %d", score); setCursorPosition(25, 14); printf("获得奖励: +50 分"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); // 黄色 setCursorPosition(22, 18); printf("按 N 进入下一关"); setCursorPosition(22, 20); printf("按 Q 返回主菜单"); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); // 白色 } // 移动敌人 void moveEnemies() { for (int i = 0; i < (level < MAX_ENEMIES ? level : MAX_ENEMIES); i++) { // 保存敌人当前位置 int oldX = enemies[i].x; int oldY = enemies[i].y; // 随机移动方向 (0:上, 1:下, 2:左, 3:右) int direction = rand() % 4; int newX = enemies[i].x; int newY = enemies[i].y; switch (direction) { case 0: newY--; break; // 上 case 1: newY++; break; // 下 case 2: newX--; break; // 左 case 3: newX++; break; // 右 } // 检查移动是否有效 if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT && maze[newY][newX] != WALL && maze[newY][newX] != EXIT) { // 更新敌人位置 enemies[i].x = newX; enemies[i].y = newY; // 恢复原位置 if (maze[oldY][oldX] == ENEMY) { maze[oldY][oldX] = PATH; } // 设置新位置 if (maze[newY][newX] != TREASURE) { maze[newY][newX] = ENEMY; } // 更新敌人显示位置 setCursorPosition(oldX + 2, oldY + 3); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); printf("%c", PATH); setCursorPosition(newX + 2, newY + 3); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); printf("%c", ENEMY); } // 检查敌人是否碰到玩家 if (enemies[i].x == player.x && enemies[i].y == player.y) { lives--; if (lives <= 0) { gameState = GAME_OVER; } else { // 玩家被击中后短暂闪烁 for (int j = 0; j < 3; j++) { setCursorPosition(player.x + 2, player.y + 3); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12); printf("%c", PLAYER); Sleep(100); setCursorPosition(player.x + 2, player.y + 3); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 14); printf("%c", PLAYER); Sleep(100); } // 将玩家移回起点 setCursorPosition(player.x + 2, player.y + 3); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); printf("%c", PATH); player.x = 1; player.y = 1; } } } } // 处理玩家输入 void processInput() { if (_kbhit()) { int key = _getch(); if (gameState == PLAYING) { if (key == 0 || key == 224) { // 方向键的第一个字符 key = _getch(); // 获取实际方向 int newX = player.x; int newY = player.y; switch (key) { case UP: newY--; break; case DOWN: newY++; break; case LEFT: newX--; break; case RIGHT: newX++; break; } // 检查移动是否有效 if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT) { if (maze[newY][newX] != WALL) { player.x = newX; player.y = newY; // 检查是否收集到宝藏 if (maze[newY][newX] == TREASURE) { score += 10; treasuresCollected++; maze[newY][newX] = PATH; // 更新宝藏位置显示 setCursorPosition(newX + 2, newY + 3); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7); printf("%c", PATH); } // 检查是否到达出口 if (newX == exitPos.x && newY == exitPos.y && treasuresCollected == MAX_TREASURES - level) { gameState = LEVEL_COMPLETE; } } } } else if (key == 'r' || key == 'R') { // 重新开始游戏 level = 1; score = 0; lives = 3; initGame(); } else if (key == 'q' || key == 'Q') { // 返回主菜单 gameState = MAIN_MENU; } } else if (gameState == MAIN_MENU) { switch (key) { case '1': // 开始游戏 gameState = PLAYING; initGame(); break; case '2': // 游戏说明 gameState = INSTRUCTIONS; break; case '3': // 关于游戏 gameState = ABOUT; break; case '4': // 退出游戏 gameRunning = 0; break; } } else if (gameState == INSTRUCTIONS || gameState == ABOUT) { gameState = MAIN_MENU; } else if (gameState == GAME_OVER || gameState == LEVEL_COMPLETE) { if (key == 'r' || key == 'R') { // 重新开始游戏 level = 1; score = 0; lives = 3; gameState = PLAYING; initGame(); } else if (key == 'q' || key == 'Q') { // 返回主菜单 gameState = MAIN_MENU; } else if (key == 'n' || key == 'N' && gameState == LEVEL_COMPLETE) { // 进入下一关 level++; score += 50; // 通关奖励 gameState = PLAYING; initGame(); } } } } // 主函数 int main() { // 设置控制台窗口大小 system("mode con cols=60 lines=30"); // 设置控制台标题 SetConsoleTitle("迷宫探险游戏"); // 隐藏光标 hideCursor(); // 初始化游戏 initGame(); // 游戏主循环 while (gameRunning) { switch (gameState) { case MAIN_MENU: drawMainMenu(); break; case PLAYING: drawGame(); moveEnemies(); break; case GAME_OVER: drawGameOver(); break; case LEVEL_COMPLETE: drawLevelComplete(); break; case INSTRUCTIONS: drawInstructions(); break; case ABOUT: drawAbout(); break; } processInput(); Sleep(100); // 控制游戏速度 } // 退出游戏前显示光标 showCursor(); system("cls"); printf("\n\n 感谢游玩迷宫探险游戏!\n\n"); return 0; }解决运行后的频闪问题告诉我完整代码
06-20
/* ********************************************************* * 多级菜单v2.0 * 作者:Adam * * 移植方法 * * 1. 配置菜单宏定义, 兼容不同显示器及布局, 例如: #define MENU_WIDTH 128 // 菜单宽度 #define MENU_HEIGHT 64 // 菜单高度 #define MENU_LINE_H 20 // 行高 * 2. 实现 menu_command_callback() 对应的指令功能以完成移植, 有些指令是有参数的, 参数已经提取好, 按需使用参数即可, * 详情可看优快云博客:https://blog.youkuaiyun.com/AdminAdam/article/details/138232161 * 使用方法 * * 1. 创建选项列表,并直接初始化, 每个选项对应其名字和功能(功能为函数指针, 直接填写函数名), 例如: static MENU_OptionTypeDef MENU_OptionList[] = { {"<<<", NULL}, // 固定格式, 用于退出 {"Tools", MENU_RunToolsMenu}, // 工具 {"Games", MENU_RunGamesMenu}, // 游戏 {"Setting", MENU_RunSettingMenu}, // 设置 {"Information", MENU_Information}, // 信息 {"..", NULL}, // 固定格式, 用于计算选项列表长度和退出 }; * 2. 创建菜单句柄 并把菜单句柄内的选项列表指针指向 第1 步创建的选项列表, 例如 static MENU_HandleTypeDef MENU = {.OptionList = MENU_OptionList}; * 3. 调用 MENU_RunMenu() 运行菜单, 参数为菜单句柄 MENU_RunMenu(&MENU); * * 4. 为了实现多级菜单, 可使用一个函数来封装 第 1 2 3 步, 封装好的函数可作为功能被其他菜单调用, 以此实现不限层级多级菜单, 此文件底部提供了示例代码 * 例如 void MENU_RunToolsMenu(void) 被选项 {"Tools", MENU_RunToolsMenu} 调用; * * * 视频教程:https://www.bilibili.com/video/BV1Y94y1g7mu?p=2 * 优快云博客:https://blog.youkuaiyun.com/AdminAdam/article/details/138232161 * * 下载链接 * 百度网盘:https://pan.baidu.com/s/1bZPWCKaiNbb-l1gpAv6QNg?pwd=KYWS * Gitee: https://gitee.com/AdamLoong/Embedded_Menu_Simple * GitHub:https://github.com/AdamLoong/Embedded_Menu_Simple * * B站UP:加油哦大灰狼 * 如果此程序对你有帮助记得给个一键三连哦! ( •̀ ω •́ )✧ ********************************************************* */ #include "MENU.h" #include "stdio.h" #include <stdarg.h> #include <stdio.h> #include <string.h> #include "usart.h" static void UART_Printf(const char *format, ...) { char tmp[128]; va_list argptr; va_start(argptr, format); vsprintf((char* )tmp, format, argptr); va_end(argptr); HAL_UART_Transmit(&huart1, (const uint8_t *)&tmp, strlen(tmp), HAL_MAX_DELAY); } /* 配置菜单 */ #define MENU_X 0 // 菜单位置X坐标 #define MENU_Y 0 // 菜单位置Y坐标 #define MENU_WIDTH 128 - 1 // 菜单宽度 #define MENU_HEIGHT 64 - 1 // 菜单高度 #define MENU_LINE_H 16 // 每行高度 #define MENU_PADDING 1 // 内边距 #define MENU_MARGIN 1 // 外边距 #define MENU_FONT_W 8 // 字体宽度 #define MENU_FONT_H 16 // 字体高度 #define MENU_BORDER 1 // 边框线条尺寸 #define IS_CENTERED 1 // 是否居中显示 #define IS_OVERSHOOT 1 // 是否启用过冲效果(果冻效果) // #define OVERSHOOT 0.321 // 过冲量 0 < 范围 < 1 #define OVERSHOOT 0.081 // 过冲量 0 < 范围 < 1 // #define ANIMATION_SPEED 0.321 // 动画速度 0 < 范围 <= 1 #define ANIMATION_SPEED 0.821 // 动画速度 0 < 范围 <= 1 // 计算可见的最大行数(光标限位) #define CURSOR_CEILING (((MENU_HEIGHT - MENU_MARGIN - MENU_MARGIN) / MENU_LINE_H) - 1) /** Port 移植接口 * **************************************************************/ /* 依赖头文件 */ #include "AdamLib_Button.h" // 按键 #include "oled.h" // 按键结果 extern int32_t result1; extern int32_t result2; extern int32_t result3; /// @brief 菜单指令回调函数 /// @param command 指令类型 /// @param ... 可变参数列表,根据指令定义 /// @return 返回值根据指令定义 int menu_command_callback(enum _menu_command command, ...) { int retval = 0; switch (command) { /* Output 输出相关指令 */ case BUFFER_DISPLAY: // 无参无返,更新显示缓冲区 { OLED_ShowFrame(); // 调用OLED更新函数 } break; case BUFFER_CLEAR: // 无参无返,清除显示缓冲区 { OLED_NewFrame(); // 调用OLED清除函数 } break; case SHOW_STRING: // 显示字符串 // 参数:( int16_t x, int16_t y, char *str ) // 返回: uint8_t 字符串长度 { /* 提取参数列表 */ int* arg_list = ((int*)&command) + 1; // 指针偏移4字节, 指向第一个参数 int show_x = arg_list[0]; // X坐标 int show_y = arg_list[1]; // Y坐标 char* show_string = (char*)arg_list[2]; // 要显示的字符串 /* 按需使用参数 */ // 调用OLED显示函数并返回字符串长度 // retval = OLED_Printf(show_x, show_y, MENU_FONT_W, show_string); OLED_PrintString(show_x, show_y, show_string, &font16x16, OLED_COLOR_NORMAL); // 如果显示函数没有返回值, 可以使用strlen()获取字符串长度 // retval = strlen(show_string); retval = strlen(show_string); } break; case SHOW_CURSOR: // 显示光标 // 参数:( int xsta, int ysta, int xend, int yend ) // 返回: 无 { /* 提取参数列表 */ int* arg_list = ((int*)&command) + 1; int cursor_xsta = arg_list[0]; // 起始X坐标 int cursor_ysta = arg_list[1]; // 起始Y坐标 int cursor_xend = arg_list[2]; // 结束X坐标 int cursor_yend = arg_list[3]; // 结束Y坐标 /* 按需使用参数 */ // 调用OLED反转区域函数显示光标 // OLED_ReverseArea(cursor_xsta, cursor_ysta, // COORD_CHANGE_SIZE(cursor_xsta, cursor_xend), // COORD_CHANGE_SIZE(cursor_ysta, cursor_yend)); // 或者使用绘制矩形的方式显示光标 OLED_DrawRectangle(cursor_xsta, cursor_ysta, COORD_CHANGE_SIZE(cursor_xsta, cursor_xend), COORD_CHANGE_SIZE(cursor_ysta, cursor_yend), 0); } break; case DRAW_FRAME: // 绘制边框 // 参数:( int xsta, int ysta, int wide, int high ) // 返回: 无 { /* 提取参数列表 */ int* arg_list = ((int*)&command) + 1; int frame_x = arg_list[0]; // X坐标 int frame_y = arg_list[1]; // Y坐标 int frame_width = arg_list[2]; // 宽度 int frame_height = arg_list[3]; // 高度 /* 按需使用参数 */ // 调用OLED绘制矩形函数 OLED_DrawRectangle(frame_x, frame_y, frame_width, frame_height, 0); } break; /* Input 输入相关指令 */ case GET_EVENT_ENTER: // 获取确认事件 // 参数: 无 // 返回: 布尔值(1表示有事件,0表示无事件) { // 确认事件可以由Enter键或Right键触发 // retval = Key_GetEvent_Enter() || Key_GetEvent_Right(); if(result3 == 1) { // UART_Printf("Enter\r\n"); retval = 1; } } break; case GET_EVENT_BACK: // 获取返回事件 // 参数: 无 // 返回: 布尔值 { // 返回事件由Back键触发(3长按) if(result3 == 9){ // UART_Printf("Back\r\n"); retval = 1; } } break; case GET_EVENT_WHEEL: // 获取滚轮/方向键事件 { // 检查方向键 if (result1 > 0) { // 单击移动1行,长按时间越长移动越快 retval = 1 + (result1 > 100 ? (result1 / 200) : 0); } else if (result2 > 0) { retval = -1 - (result2 > 100 ? (result2 / 200) : 0); } } break; // case GET_EVENT_WHEEL: // 获取滚轮/方向键事件 // // 参数: 无 // // 返回: int16_t 滚动量(正数向上,负数向下) // { // // 检查方向键 // if (result1 == 1){ // retval = 1;} // 向上 // else if (result2 == 1){ // retval = -1; } // 向下 // else // // 也可以支持编码器 // // if() // ; // // retval = Key_Encoder_Take(&Encoder1); // } // break; default: break; } return retval; } /* ***************************************************** Port 移植接口 ** */ /* ******************************************************** */ /// @brief 菜单运行函数 /// @param hMENU 菜单句柄 void MENU_RunMenu(MENU_HandleTypeDef* hMENU) { MENU_HandleInit(hMENU); // 初始化菜单句柄 // 菜单主循环 while (hMENU->isRun) { Button_Process(); // 处理按键(重要) menu_command_callback(BUFFER_CLEAR); // 擦除显示缓冲区 MENU_ShowOptionList(hMENU); /* 显示选项列表 */ MENU_ShowCursor(hMENU); /* 显示光标 */ MENU_ShowBorder(hMENU); // 显示边框 menu_command_callback(BUFFER_DISPLAY); // 将缓冲区内容更新到显示器 MENU_Event_and_Action(hMENU); // 检查用户事件并执行相应操作 } } /// @brief 初始化菜单句柄 /// @param hMENU 菜单句柄 void MENU_HandleInit(MENU_HandleTypeDef* hMENU) { hMENU->isRun = 1; // 菜单运行标志 hMENU->AnimationUpdateEvent = 1; // 动画更新事件标志 hMENU->Catch_i = 1; // 默认选中第一个有效选项(跳过"<<<") hMENU->Cursor_i = 0; // 光标初始位置 hMENU->Show_i = 0; // 显示起始索引 hMENU->Show_i_Previous = 1; // 上一次显示的起始索引 hMENU->Option_Max_i = 0; // 选项列表长度 // 计算选项列表长度(直到遇到".."结束标志) for (hMENU->Option_Max_i = 0; hMENU->OptionList[hMENU->Option_Max_i].String[0] != '.'; hMENU->Option_Max_i++) { // 获取每个选项的字符串长度 hMENU->OptionList[hMENU->Option_Max_i].StrLen = MENU_ShowOption(0, 0, &hMENU->OptionList[hMENU->Option_Max_i]); } hMENU->Option_Max_i--; // 不包含".."选项 } /// @brief 检查用户事件并执行相应操作 /// @param hMENU 菜单句柄 void MENU_Event_and_Action(MENU_HandleTypeDef* hMENU) { // 检查确认事件 if (menu_command_callback(GET_EVENT_ENTER)) { /* 如果选中的选项有功能函数则执行,否则退出菜单 */ if (hMENU->OptionList[hMENU->Catch_i].func != NULL) { hMENU->OptionList[hMENU->Catch_i].func(); // 执行选项功能 } else { hMENU->isRun = 0; // 退出菜单 } hMENU->AnimationUpdateEvent = 1; // 触发动画更新 } // 检查返回事件 else if (menu_command_callback(GET_EVENT_BACK)) { hMENU->isRun = 0; // 退出菜单 } else { // 获取滚轮/方向键事件 hMENU->Wheel_Event = -menu_command_callback(GET_EVENT_WHEEL); // 如果有滚动事件 if (hMENU->Wheel_Event) { MENU_UpdateIndex(hMENU); // 更新选中索引和光标位置 hMENU->AnimationUpdateEvent = 1; // 触发动画更新 } } } /// @brief 更新菜单索引(选中项和光标位置) /// @param hMENU 菜单句柄 void MENU_UpdateIndex(MENU_HandleTypeDef* hMENU) { /* 更新索引 */ hMENU->Cursor_i += hMENU->Wheel_Event; // 更新光标索引 hMENU->Catch_i += hMENU->Wheel_Event; // 更新选中项索引 /* 限制选中项索引范围 */ if (hMENU->Catch_i > hMENU->Option_Max_i) hMENU->Catch_i = hMENU->Option_Max_i; // 最大索引限制 if (hMENU->Catch_i < 0) hMENU->Catch_i = 0; // 最小索引限制 /* 限制光标索引范围 */ if (hMENU->Cursor_i > CURSOR_CEILING) hMENU->Cursor_i = CURSOR_CEILING; // 最大可见行限制 if (hMENU->Cursor_i > hMENU->Option_Max_i) hMENU->Cursor_i = hMENU->Option_Max_i; // 选项总数限制 if (hMENU->Cursor_i > hMENU->Catch_i) hMENU->Cursor_i = hMENU->Catch_i; // 不能超过选中项 if (hMENU->Cursor_i < 0) hMENU->Cursor_i = 0; // 最小索引限制 } /// @brief 显示选项列表 /// @param hMENU 菜单句柄 void MENU_ShowOptionList(MENU_HandleTypeDef* hMENU) { static float VerticalOffsetBuffer; // 垂直偏移缓冲(用于动画) /* 计算显示起始索引 */ hMENU->Show_i = hMENU->Catch_i - hMENU->Cursor_i; // 如果显示起始索引有变化 if (hMENU->Show_i_Previous != hMENU->Show_i) { // 计算垂直偏移量(用于滚动动画) VerticalOffsetBuffer = ((hMENU->Show_i - hMENU->Show_i_Previous) * MENU_LINE_H); hMENU->Show_i_Previous = hMENU->Show_i; // 更新上一次显示的起始索引 } // 如果有垂直偏移,逐渐归零(实现平滑滚动) if (VerticalOffsetBuffer) { VerticalOffsetBuffer = STEPWISE_TO_TARGET(VerticalOffsetBuffer, 0, ANIMATION_SPEED); } // 遍历并显示选项 for (int16_t i = -1; i <= CURSOR_CEILING + 1; i++) { // 跳过无效索引 if (hMENU->Show_i + i < 0) continue; if (hMENU->Show_i + i > hMENU->Option_Max_i) break; #if (IS_CENTERED != 0) // 水平居中显示 int16_t x = MENU_X + ((MENU_WIDTH - (hMENU->OptionList[hMENU->Show_i + i].StrLen * MENU_FONT_W)) / 2); #else // 左对齐显示(带边距) int16_t x = MENU_X + MENU_MARGIN + MENU_PADDING; #endif // 计算Y坐标(考虑垂直偏移) int16_t y = MENU_Y + MENU_MARGIN + (i * MENU_LINE_H) + ((MENU_LINE_H - MENU_FONT_H) / 2) + (int)VerticalOffsetBuffer; /* 显示选项并记录字符串长度 */ hMENU->OptionList[hMENU->Show_i + i].StrLen = MENU_ShowOption(x, y, &hMENU->OptionList[hMENU->Show_i + i]); } } /// @brief 显示单个菜单选项 /// @param X X坐标 /// @param Y Y坐标 /// @param Option 选项结构体 /// @return 字符串长度 uint8_t MENU_ShowOption(int16_t X, int16_t Y, MENU_OptionTypeDef* Option) { char String[64]; // 字符串缓冲区 // 根据变量类型格式化字符串 switch (Option->StrVarType) { case INT8: sprintf(String, Option->String, *(int8_t*)Option->StrVarPointer); break; case UINT8: sprintf(String, Option->String, *(uint8_t*)Option->StrVarPointer); break; case INT16: sprintf(String, Option->String, *(int16_t*)Option->StrVarPointer); break; case UINT16: sprintf(String, Option->String, *(uint16_t*)Option->StrVarPointer); break; case INT32: sprintf(String, Option->String, *(int32_t*)Option->StrVarPointer); break; case UINT32: sprintf(String, Option->String, *(uint32_t*)Option->StrVarPointer); break; case CHAR: sprintf(String, Option->String, *(char*)Option->StrVarPointer); break; case STRING: sprintf(String, Option->String, (char*)Option->StrVarPointer); break; case FLOAT: sprintf(String, Option->String, *(float*)Option->StrVarPointer); break; default: sprintf(String, Option->String, (void*)Option->StrVarPointer); break; } // 显示字符串并返回长度 return menu_command_callback(SHOW_STRING, X, Y, String); } /// @brief 显示光标 /// @param hMENU 菜单句柄 void MENU_ShowCursor(MENU_HandleTypeDef* hMENU) { // 实际位置变量 static float actual_xsta, actual_ysta, actual_xend, actual_yend; // 目标位置变量 static float target_xsta, target_ysta, target_xend, target_yend; #if (IS_OVERSHOOT != 0) // 过冲效果相关变量 static float bounce_xsta, bounce_ysta, bounce_xend, bounce_yend; static uint8_t bounce_cnt_xsta, bounce_cnt_ysta, bounce_cnt_xend, bounce_cnt_yend; #endif // 如果需要更新动画 if (hMENU->AnimationUpdateEvent) { hMENU->AnimationUpdateEvent = 0; // 清除更新标志 // 计算光标尺寸 uint16_t cursor_width = (MENU_PADDING + (hMENU->OptionList[hMENU->Catch_i].StrLen * MENU_FONT_W) + MENU_PADDING); uint16_t cursor_height = MENU_LINE_H; #if (IS_CENTERED != 0) // 居中显示时光标的X坐标 target_xsta = MENU_X + ((MENU_WIDTH - cursor_width) / 2); #else // 左对齐显示时光标的X坐标 target_xsta = MENU_X + MENU_MARGIN; #endif // 光标Y坐标 target_ysta = MENU_Y + MENU_MARGIN + (hMENU->Cursor_i * MENU_LINE_H); // 光标结束坐标 target_xend = SIZE_CHANGE_COORD(target_xsta, cursor_width); target_yend = SIZE_CHANGE_COORD(target_ysta, cursor_height); #if (IS_OVERSHOOT != 0) // 计算过冲目标位置 bounce_xsta = target_xsta + (target_xsta - actual_xsta) * OVERSHOOT; bounce_ysta = target_ysta + (target_ysta - actual_ysta) * OVERSHOOT; bounce_xend = target_xend + (target_xend - actual_xend) * OVERSHOOT; bounce_yend = target_yend + (target_yend - actual_yend) * OVERSHOOT; // 设置反弹次数 bounce_cnt_xsta = 2; bounce_cnt_ysta = 2; bounce_cnt_xend = 2; bounce_cnt_yend = 2; #endif } #if (IS_OVERSHOOT != 0) // 处理X方向过冲 if (bounce_xsta == actual_xsta) { if (bounce_cnt_xsta--) bounce_xsta = target_xsta + (target_xsta - actual_xsta) * OVERSHOOT; else bounce_xsta = target_xsta; } // 处理Y方向过冲 if (bounce_ysta == actual_ysta) { if (bounce_cnt_ysta--) bounce_ysta = target_ysta + (target_ysta - actual_ysta) * OVERSHOOT; else bounce_ysta = target_ysta; } // 处理X结束位置过冲 if (bounce_xend == actual_xend) { if (bounce_cnt_xend--) bounce_xend = target_xend + (target_xend - actual_xend) * OVERSHOOT; else bounce_xend = target_xend; } // 处理Y结束位置过冲 if (bounce_yend == actual_yend) { if (bounce_cnt_yend--) bounce_yend = target_yend + (target_yend - actual_yend) * OVERSHOOT; else bounce_yend = target_yend; } // 平滑过渡到过冲位置 actual_xsta = STEPWISE_TO_TARGET(actual_xsta, bounce_xsta, ANIMATION_SPEED); actual_ysta = STEPWISE_TO_TARGET(actual_ysta, bounce_ysta, ANIMATION_SPEED); actual_xend = STEPWISE_TO_TARGET(actual_xend, bounce_xend, ANIMATION_SPEED); actual_yend = STEPWISE_TO_TARGET(actual_yend, bounce_yend, ANIMATION_SPEED); #else // 直接平滑过渡到目标位置 actual_xsta = STEPWISE_TO_TARGET(actual_xsta, target_xsta, ANIMATION_SPEED); actual_ysta = STEPWISE_TO_TARGET(actual_ysta, target_ysta, ANIMATION_SPEED); actual_xend = STEPWISE_TO_TARGET(actual_xend, target_xend, ANIMATION_SPEED); actual_yend = STEPWISE_TO_TARGET(actual_yend, target_yend, ANIMATION_SPEED); #endif // 显示光标(四舍五入到整数坐标) menu_command_callback(SHOW_CURSOR, (int)(actual_xsta + 0.5), (int)(actual_ysta + 0.5), (int)(actual_xend + 0.5), (int)(actual_yend + 0.5)); } /// @brief 显示菜单边框 /// @param hMENU 菜单句柄 void MENU_ShowBorder(MENU_HandleTypeDef* hMENU) { // 绘制多层边框(根据MENU_BORDER设置) for (int16_t i = 0; i < MENU_BORDER; i++) { menu_command_callback(DRAW_FRAME, MENU_X + i, MENU_Y + i, MENU_WIDTH - i - i, MENU_HEIGHT - i - i); } } /* ******************************************************** */ /* ******************************************************** */ /* 应用示例 */ /// @brief 运行主菜单 void MENU_RunMainMenu(void) { // 定义主菜单选项列表 static MENU_OptionTypeDef MENU_OptionList[] = { {"<<<"}, // 返回上级菜单 {"Tools", MENU_RunToolsMenu}, // 工具菜单 {"Games", MENU_RunGamesMenu}, // 游戏菜单 {"Setting", NULL}, // 设置菜单 {"Information", MENU_Information}, // 信息显示 {"Setting2", NULL}, // 设置菜单 {".."} // 结束标志 }; // 创建菜单句柄 static MENU_HandleTypeDef MENU = { .OptionList = MENU_OptionList }; // 运行菜单 MENU_RunMenu(&MENU); } /// @brief 运行工具菜单 void MENU_RunToolsMenu(void) { // 定义工具菜单选项列表 static MENU_OptionTypeDef MENU_OptionList[] = { {"<<<"}, // 返回上级菜单 {"Seria", NULL}, // 串口工具 {"Oscilloscope", NULL}, // 示波器功能 {"PWM Output", NULL}, // PWM输出 {"PWM Input", NULL}, // PWM输入 {"ADC Input", NULL}, // ADC输入 {".."} // 结束标志 }; // 创建菜单句柄 static MENU_HandleTypeDef MENU = { .OptionList = MENU_OptionList }; // 运行菜单 MENU_RunMenu(&MENU); } /// @brief 运行游戏菜单 void MENU_RunGamesMenu(void) { // 定义游戏菜单选项列表 static MENU_OptionTypeDef MENU_OptionList[] = { {"<<<"}, // 返回上级菜单 {"Snake", NULL}, // 贪吃蛇游戏 {"Snake II", NULL}, // 贪吃蛇2 {"Snake III", NULL}, // 贪吃蛇3 {"Game of Life", NULL}, // 康威生命游戏 {".."} // 结束标志 }; // 创建菜单句柄 static MENU_HandleTypeDef MENU = { .OptionList = MENU_OptionList }; // 运行菜单 MENU_RunMenu(&MENU); } /// @brief 显示信息页面 void MENU_Information(void) { // 清除显示缓冲区 menu_command_callback(BUFFER_CLEAR); // 显示信息内容 menu_command_callback(SHOW_STRING, 5, 0, "menu v2.0\nBy:Adam\nbilibili\nUP:加油哦大灰狼"); // 更新显示 menu_command_callback(BUFFER_DISPLAY); // 等待用户操作 while (1) { Button_Process(); // 按Back键返回 if (menu_command_callback(GET_EVENT_BACK)) return; } } /**********************************************************/ 为什么当子菜单对应为NULL时返回上一个菜单栏时会卡死程序
最新发布
11-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值