纯c语言---大作业--实现音乐播放器

注意点

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);
}
  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夭辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值