#include <graphics.h>
#include <stdio.h>
#include <easyx.h>
// 声明button函数
void button(int x, int y, int w, int h, const char* text);
int main()
{
initgraph(640, 832, EW_SHOWCONSOLE); // 初始化图形窗口
// 如果你想要加载和显示图像,可以在这里添加代码
// 但由于问题中没有具体要求,这部分我注释掉了
IMAGE img;
loadimage(&img, "./hello.jpg");
putimage(0, 0, &img);
// 调用button函数
button(50, 50, 150, 50, "按钮");
ExMessage msg;
while(true)
{
if(peekmessage(&msg,EM_MOUSE))
{
switch(msg.message)
{
case WM_LBUTTONDOWN:
if(msg.x>=50 && msg.x<=50+150 && msg.y>=50&&msg.y<=50+50)
{
printf("我是按钮,我被点击了\n");
}
break;
default:
break;
}
}
}
// 等待用户输入后关闭图形窗口
getchar();
closegraph();
return 0;
}
// button函数的定义
void button(int x, int y, int w, int h, const char* text)
{
setbkmode(TRANSPARENT);
setfillcolor(BROWN);
fillroundrect(x, y, x + w, y + h, 10, 10);
settextstyle(30, 0, "黑体");
char text_[50] = "button"; // 这里其实不需要初始化,因为后面会覆盖
strcpy(text_, text);
int tx = x + (w - textwidth(text_)) / 2;
int ty = y + (h - textheight(text_)) / 2;
outtextxy(tx, ty, text_);
}