#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,无法保存和取消(鼠标点击按钮没有反应),请你检查鼠标处理相关的代码发现问题所在并尝试解决。
最新发布