#define _CRT_SECURE_NO_WARNINGS_
#include<graphics.h>
#include<stdio.h>
#include<string.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
struct button
{
int x;
int y;
int width;
int height;
COLORREF color;
char* ptext;
};
struct button* createButton(int x, int y, int width, int height, COLORREF color, const char* ptext)
{
struct button* pB = (struct button*)malloc(sizeof(struct button));
pB->x = x;
pB->y = y;
pB->width = width;
pB->height = height;
pB->color = color;
pB->ptext = (char*)malloc(strlen(ptext) + 1);
strcpy(pB->ptext, ptext);
return pB;
}
//rectangle(intx,inty,intxx,intyy )
void drawButton(struct button* pB)
{
setfillcolor(pB->color);
settextstyle(35, 0, "楷体");
setlinecolor(BLACK);
settextcolor(BLACK);
setbkmode(TRANSPARENT);
fillrectangle(pB->x, pB->x, pB->x + pB->width, pB->y + pB->height);
outtextxy(pB->x + 20, pB->y + 50, pB->ptext);
}
int mouseInButton(struct button*pB,MOUSEMSG m)
{
if (pB->x <= m.x && m.x <= pB->x + pB->width && pB->y <= m.y && m.y <= pB->height + pB->y)
{
pB->color = RED;
return 1;
}
pB->color = YELLOW;
return 0;
}
int clickButton(struct button * pB, MOUSEMSG m)
{
if (mouseInButton(pB, m) && m.uMsg == WM_LBUTTONDOWN)
{
return 1;
}
}
int main()
{
initgraph(800, 600);
IMAGE mm;
loadimage(&mm, "bg1.jpg", 800, 600);
struct button* play = createButton(300, 150, 180, 50, YELLOW, "播放音乐");
struct button* pause = createButton(300, 200, 180, 50, YELLOW, "暂停音乐");
drawButton(play);
drawButton(pause);
while (1) {
BeginBatchDraw();
putimage(0, 0, &mm);
drawButton(play);
drawButton(pause);
MOUSEMSG m = GetMouseMsg();
if (clickButton(play,m))
{
mciSendString("open pass.wav", 0, 0, 0);
mciSendString("play pass.wav", 0, 0, 0);
}
if (clickButton(pause, m))
{
mciSendString("pause passs.wav", 0, 0, 0);
}
Sleep(50);
EndBatchDraw();
}
closegraph();
return 0;
}
需要下载的头文件:EasyX_20210224.exe
https://easyx.cn/download/EasyX_20210224.exe