当前位置:   article > 正文

基于easyx和C++实现的鼠标交互界面(点击按钮)| 系统设计_鼠标精力界面点击

鼠标精力界面点击

实现效果一览:

当鼠标放上去后效果:

代码实现及详解:

  1. #include<iostream>
  2. using namespace std;
  3. #include <graphics.h>
  4. #include <windows.h>
  5. #include <conio.h>
  6. #include<string.h>
  7. /*
  8. * 使用easyx 制作的菜单界面
  9. */
  10. // 定义菜单按钮的宽度和高度
  11. const int BUTTON_WIDTH = 200;
  12. const int BUTTON_HEIGHT = 50;
  13. //总的操作数再加上1
  14. #define operations 11
  15. // 定义按钮的坐标和状态
  16. struct Button {
  17. int x, y; // 按钮的左上角坐标
  18. bool isHovered; // 是否被鼠标悬停
  19. };
  20. void Clear_() {
  21. system("cls");
  22. }
  23. //函数声明
  24. wchar_t* multiByteToWideChar(const string& pKey);
  25. char* wideCharToMultiByte(wchar_t* pWCStrKey);
  26. bool isInButtonArea(Button button, int x, int y);
  27. void drawButton(Button button, string str);
  28. void Show_str_to_PC(int x, int y, string str, int size);
  29. int main() {
  30. //初始化窗口
  31. initgraph(640, 660);
  32. //载入背景图片
  33. IMAGE bg_image;
  34. loadimage(&bg_image, _T("schoolmap.jpg"), 640, 660); // 加载背景图片
  35. //绘制背景图
  36. putimage(0, 0, &bg_image);
  37. //操作数组
  38. string texts[operations - 1] = { "显示景点信息\0","查找景点\0","查找两个景点间的简单路径\0","查找所有路径\0","退出系统\0","添加景点\0","删除景点\0","修改景点信息\0","添加两个景点间的路径\0","删除两个景点间的路径\0" };
  39. //用来指定显示范围的变量
  40. RECT r;
  41. // 创建按钮数组
  42. Button* buttons = new Button[operations - 1];
  43. //初始化按钮
  44. for (int i = 0; i < operations - 1; i++) {
  45. buttons[i].isHovered = false;
  46. buttons[i].x = 220;
  47. buttons[i].y = 150 + i * 50;
  48. }
  49. //绘制背景图
  50. putimage(0, 0, &bg_image);
  51. // 循环处理消息
  52. while (true) {
  53. // 绘制界面
  54. setbkmode(TRANSPARENT);
  55. settextstyle(45, 0, _T("宋体"), 0, 0, FW_BOLD, 0, 0, 0);
  56. settextcolor(WHITE);
  57. r = { 0,60, 639,120 };
  58. drawtext(_T("校园导游系统"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  59. r = { 0,100,639,160 };
  60. settextstyle(20, 0, _T("宋体"), 0, 0, FW_BOLD, 0, 0, 0);
  61. drawtext(_T("欢迎来到XXXX大学"), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  62. //遍历 画出按钮
  63. for (int i = 0; i < operations - 1; i++) {
  64. drawButton(buttons[i], texts[i]);
  65. }
  66. // 循环处理鼠标消息
  67. if (MouseHit()) {
  68. MOUSEMSG m = GetMouseMsg();
  69. switch (m.uMsg) {
  70. case WM_MOUSEMOVE:
  71. // 鼠标移动时判断是否在按钮区域内
  72. for (int i = 0; i < operations - 1; i++) {
  73. buttons[i].isHovered = isInButtonArea(buttons[i], m.x, m.y);
  74. }
  75. break;
  76. case WM_LBUTTONUP:
  77. // 左键抬起时判断是否在按钮区域内
  78. for (int i = 0; i < operations - 1; i++) {
  79. if (isInButtonArea(buttons[i], m.x, m.y)) {
  80. switch (i) {//可以在各个case中添加对应的处理函数
  81. case 0:
  82. Clear_();
  83. cleardevice();//清理屏幕
  84. cout << "显示景点信息" << endl;//这里可以用对应的函数绘制二级界面
  85. break;
  86. case 1:
  87. Clear_();
  88. cleardevice();
  89. cout << "查找景点" << endl;
  90. break;
  91. case 2:
  92. Clear_();
  93. cleardevice();
  94. cout << "查找两个景点间的简单路径" << endl;
  95. break;
  96. case 3:
  97. Clear_();
  98. cleardevice();
  99. cout << "查找所有路径" << endl;
  100. break;
  101. case 4:
  102. Clear_();
  103. cleardevice();
  104. cout << "退出系统" << endl;
  105. break;
  106. case 5:
  107. Clear_();
  108. cleardevice();
  109. cout << "添加景点" << endl;
  110. break;
  111. case 6:
  112. Clear_();
  113. cleardevice();
  114. cout << "删除景点" << endl;
  115. break;
  116. case 7:
  117. Clear_();
  118. cleardevice();
  119. cout << "修改景点" << endl;
  120. break;
  121. case 8:
  122. Clear_();
  123. cleardevice();
  124. cout << "添加景点路径" << endl;
  125. break;
  126. case 9:
  127. Clear_();
  128. cleardevice();
  129. cout << "删除景点路径" << endl;
  130. break;
  131. }
  132. putimage(0, 0, &bg_image);//重绘背景图
  133. }
  134. }
  135. }
  136. }
  137. Sleep(10);//刷新频率
  138. }
  139. closegraph();
  140. return 0;
  141. }
  142. //作此界面可能需要的函数
  143. /*wchart 转string 函数*/
  144. char* wideCharToMultiByte(wchar_t* pWCStrKey)
  145. {
  146. //第一次调用确认转换后单字节字符串的长度,用于开辟空间
  147. int pSize = WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), NULL, 0, NULL, NULL);
  148. char* pCStrKey = new char[pSize + 1];
  149. //第二次调用将双字节字符串转换成单字节字符串
  150. WideCharToMultiByte(CP_OEMCP, 0, pWCStrKey, wcslen(pWCStrKey), pCStrKey, pSize, NULL, NULL);
  151. pCStrKey[pSize] = '\0';
  152. return pCStrKey;
  153. }
  154. /*string 转 wchar 函数*/
  155. wchar_t* multiByteToWideChar(const string& pKey)
  156. {
  157. const char* pCStrKey = pKey.c_str();
  158. //第一次调用返回转换后的字符串长度,用于确认为wchar_t*开辟多大的内存空间
  159. int pSize = MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, NULL, 0);
  160. wchar_t* pWCStrKey = new wchar_t[pSize];
  161. //第二次调用将单字节字符串转换成双字节字符串
  162. MultiByteToWideChar(CP_OEMCP, 0, pCStrKey, strlen(pCStrKey) + 1, pWCStrKey, pSize);
  163. return pWCStrKey;
  164. }
  165. // 判断是否在按钮区域内
  166. bool isInButtonArea(Button button, int x, int y) {
  167. return (x >= button.x && x <= button.x + BUTTON_WIDTH
  168. && y >= button.y && y <= button.y + BUTTON_HEIGHT);
  169. }
  170. // 绘制按钮
  171. void drawButton(Button button, string str) {
  172. if (button.isHovered) {
  173. setfillcolor(WHITE); settextcolor(RGB(59, 90, 166));
  174. }
  175. else {
  176. setfillcolor(RGB(59, 90, 166));
  177. settextcolor(WHITE);
  178. }
  179. fillroundrect(button.x, button.y, button.x + BUTTON_WIDTH, button.y + BUTTON_HEIGHT, 10, 10);
  180. RECT r = { button.x, button.y, button.x + BUTTON_WIDTH, button.y - 1 + BUTTON_HEIGHT - 1 };
  181. setbkmode(TRANSPARENT);
  182. settextstyle(20, 0, _T("微软雅黑"), 0, 0, FW_BOLD, 0, 0, 0);
  183. drawtext(multiByteToWideChar(str), &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  184. }
  185. //输出指定字符串到屏幕指定位置
  186. void Show_str_to_PC(int x, int y, string str, int size) {
  187. setbkmode(TRANSPARENT);
  188. settextstyle(size, 0, _T("宋体"), 0, 0, FW_BOLD, 0, 0, 0);
  189. settextcolor(WHITE);
  190. outtextxy(x, y, multiByteToWideChar(str));
  191. }

欢迎讨论!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/455833
推荐阅读
相关标签
  

闽ICP备14008679号