C语言实现音乐播放器的示例代码

  //

  // 编译该范例前,请把 music.mp3 放在项目文件夹中

  // 把 music.mp3 和编译的 exe 放在一起

  // 编译环境:VC6~VC2019 + EasyX_20210115

  //

  #include

  #include

  #include

  // 引用 Windows Multimedia API

  #pragma comment(lib, "Winmm.lib")

  void ZT(); // 暂停

  void BF1(); // 播放1

  void BF2(); // 播放2

  void BF3(); // 播放3

  void menu(); // 总得

  void locate(int a, int b, int c, int d, int e, LPCTSTR text);

  void locate(int a, int b, int c, int d, int e, LPCTSTR text, COLORREF color);

  int Return();

  int main()

  {

  initgraph(640, 480);

  while (1)

  {

  int A=Return(); // 鼠标选择;

  switch (A)

  {

  case 0:ZT();break;

  case 1:BF1();break;

  case 2:BF2();break;

  case 3:BF3();break;

  }

  }

  closegraph();

  return 0;

  }

  void ZT() // 暂停

  {

  mciSendString(_T("stop mymusic"), NULL, 0, NULL);

  mciSendString(_T("close mymusic"), NULL, 0, NULL);

  }

  void BF1() // 播放1

  {

  mciSendString(_T("open 1.mp3 alias mymusic"), NULL, 0, NULL);

  // 播放音乐

  mciSendString(_T("play mymusic"), NULL, 0, NULL);

  }

  void BF2() // 播放2

  {

  mciSendString(_T("open 2.mp3 alias mymusic"), NULL, 0, NULL);

  // 播放音乐

  mciSendString(_T("play mymusic"), NULL, 0, NULL);

  }

  void BF3() // 播放3

  {

  mciSendString(_T("open 1.mp3 alias mymusic"), NULL, 0, NULL);

  // 播放音乐

  mciSendString(_T("play mymusic"), NULL, 0, NULL);

  }

  int Return()

  {

  menu();

  MOUSEMSG m1;

  //BeginBatchDraw();

  while (1)

  {

  m1 = GetMouseMsg();

  switch (m1.uMsg)

  {

  case WM_MOUSEMOVE:

  menu();

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 100 && m1.y <= 130)

  {

  menu();

  locate(220, 100, 420, 130, 30, _T("暂停/结束"), RED);

  break;

  }

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 130 && m1.y <= 160)

  {

  menu();

  locate(220, 130, 420, 160, 30, _T("播放音乐1"), RED);

  break;

  }

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 160 && m1.y <= 190)

  {

  menu();

  locate(220, 160, 420, 190, 30, _T("播放音乐2"), RED);

  }

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 190 && m1.y <= 220)

  {

  menu();

  locate(220, 190, 420, 220, 30, _T("播放音乐3"), RED);

  }

  }

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 100 && m1.y <= 130)

  {

  locate(220, 100, 420, 130, 30, _T("暂停/结束"), RED);

  if (m1.uMsg == WM_LBUTTONDOWN)

  {

  return 0;

  }

  }

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 130 && m1.y <= 160)

  {

  if (m1.uMsg == WM_LBUTTONDOWN)

  {

  return 1; //

  }

  }

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 160 && m1.y <= 190)

  {

  if (m1.uMsg == WM_LBUTTONDOWN)

  {

  return 2;

  }

  }

  if (m1.x >= 220 && m1.x <= 420 && m1.y >= 190 && m1.y <= 220)

  {

  if (m1.uMsg == WM_LBUTTONDOWN)

  {

  return 3;

  }

  }

  }

  }

  void locate(int a, int b, int c, int d, int e, LPCTSTR text, COLORREF color)

  {

  HCURSOR hcur = LoadCursor(NULL, IDC_CROSS); // 加载系统预置的鼠标样式

  HWND hwnd = GetHWnd(); // 获取绘图窗口句柄

  SetClassLongPtr(hwnd, GCLP_HCURSOR, (long)hcur); // 设置窗口类的鼠标样式

  setlinecolor(color);

  roundrect(a, b, c, d, e, e);

  settextcolor(color);

  settextstyle(20, 0, _T("黑体"));

  setbkmode(TRANSPARENT);

  char arr[50] = "按钮";

  RECT r = { a, b, c, d };

  drawtext(text, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

  }

  void locate(int a, int b, int c, int d, int e, LPCTSTR text)

  {

  setlinecolor(WHITE);

  roundrect(a, b, c, d, e, e);

  settextcolor(YELLOW);

  settextstyle(20, 0, _T("黑体"));

  setbkmode(TRANSPARENT);

  char arr[50] = "按钮";

  RECT r = { a, b, c, d };

  drawtext(text, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

  }

  void menu()

  {

  HCURSOR hcur = LoadCursor(NULL, IDC_ARROW); // 加载系统预置的鼠标样式

  HWND hwnd = GetHWnd(); // 获取绘图窗口句柄

  SetClassLongPtr(hwnd, GCLP_HCURSOR, (long)hcur); // 设置窗口类的鼠标样式

  locate(220, 100, 420, 130, 30, _T("暂停/结束"));

  locate(220, 130, 420, 160, 30, _T("播放音乐1"));

  locate(220, 160, 420, 190, 30, _T("播放音乐2"));

  locate(220, 190, 420, 220, 30, _T("播放音乐3"));

  }