_tcscpy

使用_tcscpy代替strcpy、wcscpy、lstrcpy 


好处:是可以不管是用unicode 编码还是其他 ,代码都不用改.


C++标准库函数提供了字符和字符串的操作函数,并提供了其UNICODE版本,如:


lstrcpy 是windows API 函数其他的都是C库函数,既能作用于ASCII又能用于UNICODE

函数功能:该函数复制一个字符串到缓冲区
函数原型:LPTSTR lstrcpy(LPTSTR lpString1,LPCTSTR lpString2);
参数:
lpString1:指向接收由参数lpString2指向字符串内容的缓冲区。缓冲区必须足够大来容纳字符串,还包括最后的NULL终止符。
lpString2:指向待复制的以NULL为终止符的字符串。
返回值:若函数运行成功,返回值是缓冲区的指针;若函数运行失败,返回值是NULL。若想获得更多错误信息,请调用GetLastError函数。
注意:使用系统的双字节字符设置(DBCS)版本,此函数可以复制DBCS字符串。
速查:Windows NT 3.1、Windows 95以上,头文件:winbase.h;库文件:kernel32.lib;Unicode;在Windows NT上实现为Unicode和ANSI两种版本。


strcpy函数

原型声明:char *strcpy(char* dest, const char *src);
头文件:#include <string.h> 和 #include <stdio.h>
功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间
说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串。
返回指向dest的指针。

wcscpy函数(宽字符

原型声明:wchar_t *wcscpy(wchar_t *restrict ws1, const wchar_t *restrict ws2);
头文件:#include <wchar.h>
功能:把从ws2地址开始且含有NULL结束符的字符串复制到以ws1开始的地址空间
说明:ws2和ws1所指内存区域不可以重叠且ws1必须有足够的空间来容纳ws2的字符串。
返回指向ws1的指针。



_tscpy是一种兼容函数,当定义了UNICODE(_UNICODE)时为wcscpy,没定义UNICODE(_UNICODE)时为strcpy

#ifdef  UNICODE   
    #define _tcscpy     wcscpy  
#else  
    #define _tcscpy     strcpy  
#endif 



strcpy_s函数

下是使用strcpy_s与strcpy的安全性比较

char szBuf[2] = {0};

strcpy_s(szBuf, 2, "12131"); //新的CRT函数
strcpy(szBuf, "12131");    //老的CRT函数

上述代码,明显有缓冲区溢出的问题。 使用strcpy_s函数则会抛出一个异常。而使用strcpy函数的结果则未定,因为它错误地改变了程序中其他部分的内存的数据,可能不会抛出异常但导致程序数据错误,也可能由于非法内存访问抛出异常。

使用新的增强安全的CRT函数有什么好处呢?简单地说,新的函数加强了对参数合法性的检查以及缓冲区边界的检查,如果发现错误,会返回errno或抛出异常。老版本的这些CRT函数则没有那么严格的检查与校验,如果错误地传输了参数或者缓冲区溢出,那么错误并不能被立刻发现,对于定位程序错误也带来更大困难。




#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值