前言
还是介绍这个库函数
ExMessage msg = { 0 };
bool button(int x, int y, int w, int h, const char* text)
{
//绘制按钮
setfillcolor(RGB(230, 231, 232));
fillroundrect(x, y, x + w, y + h, 5, 5);
if ((msg.x > x && msg.x<x + w && msg.y>y && msg.y < y + h))//给按钮变色
{
settextcolor(RED);
}
else
settextcolor(BLACK);
//绘制文本
int Hspace = (w - textwidth(text)) / 2;
int Vspace = (h - textheight(text)) / 2;
outtextxy(x + Hspace, y + Vspace, text);
//判断按钮是否被点击
if ((msg.x > x && msg.x<x + w && msg.y>y && msg.y < y + h)&&msg.message==WM_LBUTTONDOWN)
{
return true;
}
return false;
}
int i = 0;
int main()
{
initgraph(640, 480, EX_SHOWCONSOLE | EX_DBLCLKS);
setbkcolor(RGB(231, 114, 227));
cleardevice();
//设置背景模式
setbkmode(TRANSPARENT);
while (true)
{
//双缓冲绘图
BeginBatchDraw();
//获取信息
peekmessage(&msg, EX_MOUSE);
if (button(20, 20, 150, 35, "start game"))
{
printf("start game %d\n",i++);
}
if (button(300, 20, 150, 35, "end game"))
{
printf("end game %d\n",i++);
}
EndBatchDraw();
//清空消息 因为每次获取消息是只会停留在原消息之上,除非你变换鼠标
msg.message = 0;
}
return 0;
}
//绘制按钮可以用来做界面 okk
总结
仍然是代码重要,其中的获取信息很关键