C++实现简单的按钮交互

#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

  • 9
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值