注意点
1,<graphics.h>需要自己下载
2,在使用vs2019时需要改一下设置
项目->属性->使用多字节字符集
实现过于匆忙,草率,代码略显粗糙,望读者海涵,毕竟我是萌新弱鸡。
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include<stdio.h>
#include<windows.h>
#include<mmsystem.h>//多媒体库
#include<conio.h>
#include<string>
#include<string.h>
#include<graphics.h>//图形库
#pragma comment(lib, "Winmm.lib")
typedef struct Button//建立我的按钮
{
int x;
int y;
int xx;
int yy;
COLORREF color;
char* buttonStr;
}BTN,*LPBTN;
LPBTN create_Button(int x, int y, int xx, int yy, COLORREF color, char* buttonStr)//定义button坐标结构体
{
LPBTN button = (LPBTN)malloc(sizeof(BTN));
//坐标信息
button->x = x;
button->y = y;
button->xx = xx;
button->yy = yy;
button->color = color;
button->buttonStr = (char*)malloc(strlen(buttonStr) + 1);
strcpy(button->buttonStr, buttonStr);
return button;
}
//void go_music();//播放音乐
void drawButton(LPBTN drybut);//画button
int isinbutton(LPBTN isbut, MOUSEMSG m)//判断鼠标是否在这个isbut的上面
{
if (isbut->x <= m.x && m.x <= isbut->xx && isbut->y <= m.y && isbut->yy >= m.y)
{
return 1;
}
return 0;
}
void play_Mouse(LPBTN play, MOUSEMSG m);//当鼠标移动到我们的button上面的时候,我们改变button的color
int clickButton(LPBTN but, MOUSEMSG m);//点击屏幕
void click(int flag, LPBTN play, LPBTN pause, LPBTN resume, LPBTN close, MOUSEMSG m);
int main()
{
//加载并添加图片
initgraph(800, 600);
int num = 1;
IMAGE background;
loadimage(&background, "3.jpg", 800, 600);
putimage(0, 0, &background);
//打开我的音乐
mciSendString("open temp.mp3", 0, 0, 0);
mciSendString("open 1.mp3", 0, 0, 0);
mciSendString("open 2.mp3", 0, 0, 0);
mciSendString("open 3.mp3", 0, 0, 0);
mciSendString("open 4.mp3", 0, 0, 0);
char ch;
//定义我的按钮们
LPBTN play = create_Button(10, 200, 160, 250,YELLOW,"播放音乐");
LPBTN pause = create_Button(10, 260, 160, 310, YELLOW, "暂停音乐");
LPBTN resume = create_Button(10, 320, 160, 370, YELLOW, "继续音乐");
LPBTN next = create_Button(10, 380, 160, 430, YELLOW, "下一首");
LPBTN close = create_Button(10, 440, 160, 490, YELLOW, "关闭音乐");
int flag = 0;//通过flag来切换歌曲。
while (1) //执行播放器
{
//画出我们的button
BeginBatchDraw();
cleardevice();
putimage(0, 0, &background);
drawButton(play);
drawButton(pause);
drawButton(resume);
drawButton(close);
drawButton(next);
//获得鼠标信息
MOUSEMSG m = GetMouseMsg();
play_Mouse(play, m);
play_Mouse(pause, m);
play_Mouse(resume, m);
play_Mouse(close, m);
play_Mouse(next, m);
EndBatchDraw();
if (clickButton(next, m))//切换音乐
{
if (flag == 0)
{
mciSendString("close temp.mp3", 0, 0, 0);
mciSendString("play 1.mp3", 0, 0, 0);
}
else if (flag == 1)
{
mciSendString("close 1.mp3", 0, 0, 0);
mciSendString("play 2.mp3", 0, 0, 0);
}
else if (flag == 2)
{
mciSendString("close 2.mp3", 0, 0, 0);
mciSendString("play 3.mp3", 0, 0, 0);
}
else if (flag == 3)
{
mciSendString("close 3.mp3", 0, 0, 0);
mciSendString("play 4.mp3", 0, 0, 0);
}
else if (flag == 4)
{
mciSendString("close 4.mp3", 0, 0, 0);
mciSendString("play temp.mp3", 0, 0, 0);
}
flag++;
flag = flag % 5;
}
click(flag, play, pause, resume, close, m);
}
ch = getchar();///暂停,防止程序结束
return 0;
}
void add_background()
{
initgraph(800, 600);
int num = 1;
IMAGE background;
loadimage(&background, "3.jpg", 800, 600);
putimage(0, 0, &background);
char ch;
}
void add_background1()
{
initgraph(800, 600);
int num = 1;
IMAGE background;
loadimage(&background, "3.jpg", 800, 600);
putimage(0, 0, &background);
char ch;
}
void play_Mouse(LPBTN play, MOUSEMSG m)
{
if (isinbutton(play, m))
{
play->color = RED;
}
else play->color = GREEN;
}
int clickButton(LPBTN but, MOUSEMSG m)
{
if (isinbutton(but, m))
{
//windows message left button dowm
if (m.uMsg == WM_LBUTTONDOWN)
{
return 1;
}
}
return 0;
}
void click(int flag, LPBTN play, LPBTN pause, LPBTN resume, LPBTN close, MOUSEMSG m)
{
if (flag == 0)
{
if (clickButton(play, m))
{
mciSendString("play temp.mp3", 0, 0, 0);
}
if (clickButton(pause, m))
{
mciSendString("pause temp.mp3", 0, 0, 0);
}
if (clickButton(resume, m))
{
mciSendString("resume temp.mp3", 0, 0, 0);
}
if (clickButton(close, m))
{
mciSendString("close temp.mp3", 0, 0, 0);
}
}
else if (flag == 1)
{
if (clickButton(play, m))
{
mciSendString("play 1.mp3", 0, 0, 0);
}
if (clickButton(pause, m))
{
mciSendString("pause 1.mp3", 0, 0, 0);
}
if (clickButton(resume, m))
{
mciSendString("resume 1.mp3", 0, 0, 0);
}
if (clickButton(close, m))
{
mciSendString("close 1.mp3", 0, 0, 0);
}
}
else if (flag == 2)
{
if (clickButton(play, m))
{
mciSendString("play 2.mp3", 0, 0, 0);
}
if (clickButton(pause, m))
{
mciSendString("pause 2.mp3", 0, 0, 0);
}
if (clickButton(resume, m))
{
mciSendString("resume 2.mp3", 0, 0, 0);
}
if (clickButton(close, m))
{
mciSendString("close 2.mp3", 0, 0, 0);
}
}
else if (flag == 3)
{
if (clickButton(play, m))
{
mciSendString("play 3.mp3", 0, 0, 0);
}
if (clickButton(pause, m))
{
mciSendString("pause 3.mp3", 0, 0, 0);
}
if (clickButton(resume, m))
{
mciSendString("resume 3.mp3", 0, 0, 0);
}
if (clickButton(close, m))
{
mciSendString("close 3.mp3", 0, 0, 0);
}
}
else if (flag == 4)
{
if (clickButton(play, m))
{
mciSendString("play 4.mp3", 0, 0, 0);
}
if (clickButton(pause, m))
{
mciSendString("pause 4.mp3", 0, 0, 0);
}
if (clickButton(resume, m))
{
mciSendString("resume 4.mp3", 0, 0, 0);
}
if (clickButton(close, m))
{
mciSendString("close 4.mp3", 0, 0, 0);
}
}
}
void drawButton(LPBTN drybut)
{
setlinecolor(RED);
setfillcolor(drybut->color);
fillrectangle(drybut->x, drybut->y, drybut->xx, drybut->yy);
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, "楷体");
outtextxy(drybut->x + 15, drybut->y + 10, drybut->buttonStr);
}