经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++音乐播放按钮的封装过程详解
来源:jb51  时间:2022/8/15 18:59:43  对本文有异议

1、准备工作:音乐、开发工具VS stdio及图形库工具

2、设计思路:先加载音乐,再通过点击不同的按钮执行不同的操作(播放音乐,暂停音乐、继续播放、结束播放)

绘制按钮我们通过一个按钮button类来操作,这样数据会存在一些必要的访问数据权限,并可以将多个函数声明写在同一个类中,调用只需使用 " 类名.函数名 “即可调用里面的函数

按钮类头文件:-----button.h

  1. #include "graphics.h"
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5. class Button
  6. {
  7. public:
  8. void Show();
  9. void InitButton(int xx, int yy, int ww, int hh, COLORREF color, string text);
  10. bool InButton(ExMessage message);
  11. bool OnClickButton(ExMessage message);
  12. private:
  13. int x;
  14. int y;
  15. int w;
  16. int h;
  17. COLORREF curColor;
  18. COLORREF oldColor;
  19. string str;
  20. };

写类中函数的定义(即写函数的函数体) ----button.cpp

注意:在类外写类内部函数的定义时,需要加类名限定

1、初始化按钮的一些参数:如按钮的长宽高、颜色和按钮上的文字内容:

  1. void Button::InitButton(int xx, int yy, int ww, int hh, COLORREF color, string text)
  2. {
  3. x = xx;
  4. y = yy;
  5. w = ww;
  6. h = hh;
  7. curColor = color;
  8. oldColor = color;
  9. str = text;
  10. }

2、绘制矩形按钮:

  1. void Button::Show()
  2. {
  3. //矩形框
  4. setfillcolor(curColor);
  5. solidrectangle(x, y, x + w, y + h);
  6. //文字
  7. settextstyle(15, 0, "FZZJ-XHFTJW.TTF");
  8. //1、求文字所在矩形的宽高
  9. int textw = textwidth(str.c_str());
  10. int texth = textheight(str.c_str());
  11. //2、求h1 w1
  12. //(w - textw) / 2 <=> w1
  13. //(h - texth) / 2 <=> h1
  14. //3、求出文字所在矩形左上角的坐标
  15. int xx = x+(w - textw) / 2;
  16. int yy = y+(h - texth) / 2;
  17. setbkmode(TRANSPARENT);
  18. settextcolor(BLACK);
  19. outtextxy(xx, yy, str.c_str());
  20. }

注意:这里有一个文字在矩形框中居中显示的:

如何将文字在矩形框中居中显示?

如图:要使文字在矩形中居中显示: h1=h2 ; w1=w2

步骤:

1、求出文字的 高(textwidth(文字)) 与 宽(text(文字)) 返回的是一个整数

2、求出h1、w1的值

3、外矩形的宽高分别加上w1,h1就是需要绘制里面文字所在的矩形框的左上角的坐标。绘制一个矩形只需直到矩形左上角的坐标和矩形的宽高即可绘制绘制一个矩形。

4、判断鼠标是否在按钮中---在矩形中矩形显示一种颜色,不在显示另外一种颜色

  1. bool Button::InButton(ExMessage message)
  2. {
  3. if (message.x >= x && message.x <= x + w && message.y >= y && message.y <= y + h)
  4. {
  5. curColor = RGB(236, 244, 255);
  6. return true;
  7. }
  8. curColor = oldColor;
  9. return false;
  10. }

5、判断鼠标是否点击矩形框

  1. bool Button::OnClickButton(ExMessage m)
  2. {
  3. if (InButton(m) && m.message == WM_LBUTTONDOWN)
  4. {
  5. return true;
  6. }
  7. return false;
  8. }

主函数---main

加载音乐,绘制按钮,按钮消息的制作,显示界面等

  1. #include "button.h"
  2. #include <mmsystem.h>
  3. #pragma comment(lib,"winmm.lib")
  4. int main()
  5. {
  6. initgraph(800, 600);
  7. IMAGE mm;
  8. loadimage(&mm, "mm.jpg",800,600);
  9. Button* play = new Button;
  10. play->InitButton(5, 5, 100, 25, RGB(204, 213, 240), "播放(play)");
  11. Button* pause = new Button;
  12. pause->InitButton(110, 5, 100, 25, RGB(204, 213, 240), "暂停(pause)");
  13. Button* resume = new Button;
  14. resume->InitButton(215, 5, 100, 25, RGB(204, 213, 240), "继续(resume)");
  15. Button* stop = new Button;
  16. stop->InitButton(320, 5, 100, 25, RGB(204, 213, 240), "停止(stop)");
  17. ExMessage m;
  18. BeginBatchDraw();
  19. while (1)
  20. {
  21. putimage(0, 0, &mm);
  22. peekmessage(&m);
  23. play->Show();
  24. if (play->OnClickButton(m))
  25. {
  26. mciSendString("open 1.mp3", 0, 0, 0);
  27. mciSendString("play 1.mp3", 0, 0, 0);
  28. }
  29. pause->Show();
  30. if (pause->OnClickButton(m))
  31. {
  32. mciSendString("pause 1.mp3", 0, 0, 0);
  33. }
  34. resume->Show();
  35. if (resume->OnClickButton(m))
  36. {
  37. mciSendString("resume 1.mp3", 0, 0, 0);
  38. }
  39. stop->Show();
  40. if (stop->OnClickButton(m))
  41. {
  42. mciSendString("close 1.mp3", 0, 0, 0);
  43. }
  44. FlushBatchDraw();
  45. }
  46. EndBatchDraw();
  47. closegraph();
  48. return 0;
  49. }

到此这篇关于C++音乐播放按钮的封装过程详解的文章就介绍到这了,更多相关C++封装内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号