代码:
#include "workerManager.h"
#include "manager.h"
#include "worker.h"
#include "technician.h"
#include <iostream>
#include <graphics.h>
#include <windows.h>
#include <conio.h>
#include <string.h>using namespace std;// 定义菜单按钮的宽度和高度
const int BUTTON_WIDTH = 200;
const int BUTTON_HEIGHT = 50;// 按钮个数,总的操作数再加上 1
#define operations 7// 定义按钮的坐标和状态
struct Button {int x, y; // 按钮的左上角坐标bool isHovered; // 是否被鼠标悬停
};// 清屏函数
void Clear_() {system("cls");
}// 多字节字符编码转宽字符编码函数
wchar_t* multiByteToWideChar(const string& pKey) {// 第一次调用确认转换后单字节字符串的长度,用于开辟空间int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), NULL, 0, NULL, NULL);wchar_t* pWCStrKey = new wchar_t[pSize + 1];// 第二次调用将双字节字符串转换成单字节字符串WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), pWCStrKey, pSize, NULL, NULL);pWCStrKey[pSize] = '\0';return pWCStrKey;
}// 宽字符编码转多字节字符编码函数
char* wideCharToMultiByte(wchar_t* pWCStrKey) {// 第一次调用返回转换后的字符串长度,用于确认为 wchar_t* 开辟多大的内存空间int pSize = MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, NULL, 0);char* pCStrKey = new char[pSize + 1];// 第二次调用将单字节字符串转换成双字节字符串MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, pCStrKey, pSize);pCStrKey[pSize] = '\0';return pCStrKey;
}// 判断是否在按钮区域内
bool isInButtonArea(Button button, int x, int y) {return (x >= button.x && x <= button.x + BUTTON_WIDTH&& y >= button.y && y <= button.y + BUTTON_HEIGHT);
}// 绘制按钮
void drawButton(Button button, string str) {if (button.isHovered) {setfillcolor(WHITE);settextcolor(RGB(59, 90, 166));}else {setfillcolor(RGB(59, 90, 166));settextcolor(WHITE);}fillroundrect(button.x, button.y, button.x + BUTTON_WIDTH, button.y + BUTTON_HEIGHT, 10, 10);RECT r = { button.x, button.y, button.x + BUTTON_WIDTH, button.y - 1 + BUTTON_HEIGHT - 1 };setbkmode(TRANSPARENT);settextstyle(20, 0, _T("微软雅黑"), 0, 0, FW_BOLD, 0, 0, 0);drawtext(multiByteToWideChar(str), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}// 输出指定字符串到屏幕指定位置
void Show_str_to_PC(int x, int y, string str, int size) {setbkmode(TRANSPARENT);settextstyle(size, 0, _T("宋体"), 0, 0, FW_BOLD, 0, 0, 0);settextcolor(WHITE);outtextxy(x, y, multiByteToWideChar(str));
}int main() {// 初始化窗口initgraph(640, 660);setbkcolor(WHITE);// 载入背景图片IMAGE bg_image;loadimage(&bg_image, _T("cqcu.jpg"), 640, 660); // 加载背景图片// 绘制背景图putimage(0, 0, &bg_image);// 操作数组string texts[operations - 1] = { "显示所有员工信息\0", "修改员工信息\0", "录入员工信息\0", "删除员工信息\0", "查询员工信息\0", "退出员工系统\0" };// 用来指定显示范围的变量RECT r;// 创建按钮数组Button* buttons = new Button[operations - 1];// 初始化按钮for (int i = 0; i < operations - 1; i++) {buttons[i].isHovered = false;buttons[i].x = 220;buttons[i].y = 150 + i * 50;}// 绘制背景图putimage(0, 0, &bg_image);// 循环处理消息while (true) {// 绘制界面setbkmode(TRANSPARENT);settextstyle(45, 0, _T("宋体"), 0, 0, FW_BOLD, 0, 0, 0);settextcolor(BLACK);r = { 0, 60, 639, 120 };drawtext(_T("员工管理系统"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);r = { 0, 100, 639, 160 };settextstyle(20, 0, _T("宋体"), 0, 0, FW_BOLD, 0, 0, 0);drawtext(_T("计算机与信息科学学院"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);// 遍历画出按钮for (int i = 0; i < operations - 1; i++) {drawButton(buttons[i], texts[i]);}// 循环处理鼠标消息if (MouseHit()) {MOUSEMSG m = GetMouseMsg();switch (m.uMsg) {case WM_MOUSEMOVE:// 鼠标移动时判断是否在按钮区域内for (int i = 0; i < operations - 1; i++) {buttons[i].isHovered = isInButtonArea(buttons[i], m.x, m.y);}break;case WM_LBUTTONUP:// 左键抬起时判断是否在按钮区域内for (int i = 0; i < operations - 1; i++) {if (isInButtonArea(buttons[i], m.x, m.y)) {switch (i) {// 可以在各个 case 中添加对应的处理函数case 0:Clear_();cleardevice();// 清理屏幕cout << "显示所有员工信息" << endl;// 这里可以用对应的函数绘制二级界面break;case 1:Clear_();cleardevice();cout << "录入员工信息" << endl;break;case 2:Clear_();cleardevice();cout << "查询员工信息" << endl;break;case 3:Clear_();cleardevice();cout << "修改员工信息" << endl;break;case 4:Clear_();cleardevice();cout << "删除员工信息" << endl;break;case 5:Clear_();cleardevice();cout << "排序员工信息" << endl;break;case 6:Clear_();cleardevice();cout << "退出系统" << endl;break;}putimage(0, 0, &bg_image);// 重绘背景图}}}Sleep(10);// 刷新频率}closegraph();}return 0;
}
运行结果:
在贴图的位置可以自行更换背景