easyx按钮交互出现bug

试图实现一个鼠标移动到按钮上按钮就变色的程序,但是我封装好的按钮最开始不会显示,鼠标一移到界面内就四个按钮一起出现然后变色

img

img


源代码在这
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
#include
#pragma comment(lib,"winmm.lib")

struct button
{
int x;
int y;
int w;
int h;
COLORREF color;
char* pText;
};
//初始化按钮属性
struct button* createbutton(int x, int y, int w, int h, COLORREF color, const char* pText)
{
struct button* pB = (struct button*)malloc(sizeof(struct button));
pB->x = x;
pB->y = y;
pB->w = w;
pB->h = h;
pB->color = color;
pB->pText = (char*)malloc(strlen(pText) + 1);
strcpy(pB->pText, pText);
return pB;
}
//画出按钮
void drawButton(struct button* pB)
{
setfillcolor(pB->color);
settextstyle(35, 0, "幼圆");
setlinecolor(BLACK);
settextcolor(BLACK);
setbkmode(TRANSPARENT);
fillrectangle(pB->x, pB->y, pB->x + pB->w, pB->y + pB->h);
outtextxy(pB->x + 20, pB->y + 10, pB->pText);

}
int mouseInButton(struct button* pB, MOUSEMSG m)
{
if (pB->x <= m.x && m.x <= pB->x + pB->w && pB->y <= m.y && m.y <= pB->y + pB->h);
{
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;
}
return 0;
}*/

int main()
{
initgraph(800, 600);
//mciSendString("open eufonius.mp3", 0, 0, 0);
//mciSendString("play eufonius.mp3", 0, 0, 0);

IMAGE k;
loadimage(&k, "fec49f59b98041a4a16886893447f746.jpeg", 800,600);
//putimage(0, 0, &k);
/ttextcolor(YELLOW);
/ttextstyle(100, 0, "方正舒体");
/tbkmode(TRANSPARENT);
//outtextxy(250, 50, "万年历");

struct button* enter = createbutton(300, 200, 190, 50, YELLOW, "进入日历");
struct button* mood = createbutton(300, 275, 190, 50, YELLOW, "心情系统");
struct button* time = createbutton(300, 350, 190, 50, YELLOW, "日程提醒");
struct button* close = createbutton(300, 425, 190, 50, YELLOW, "关闭程序");

while (1)
{
    BeginBatchDraw();
    putimage(0, 0, &k);

    drawButton(enter);
    drawButton(mood);
    drawButton(time);
    drawButton(close);
    MOUSEMSG m = GetMouseMsg();

    if (mouseInButton(enter, m))
    {

    }

    if (mouseInButton(mood, m))
    {

    }

    if (mouseInButton(time, m))
    {

    }

    if (mouseInButton(close, m))
    {

    }
    EndBatchDraw();

}

getchar();
return 0;

}
/*while (1)
{
MOUSEMSG m = GetMouseMsg();
switch (m.uMsg)
{
case WM_LBUTTONDOWN:
circle(m.x, m.y, 10);
break;
case WM_RBUTTONDOWN:
rectangle(m.x - 5, m.y - 5, m.x + 5, m.y + 5);
break;
}

}*/

/*
while (true)
{
ExMessage msg;
while (peekmessage(&msg,EM_MOUSE))
{
startupScene(&msg);
solidrectangle(0, 0, 50, 50);

    setfillcolor(YELLOW);

    if (isInRect(&msg, 0, 0, 50, 50))
    {
        setfillcolor(RED);
    }
    solidrectangle(0, 0, 50, 50);
}

}
*/

//EndBatchDraw();
//BeginBatchDraw();
/*
bool isInRect(ExMessage* msg, int x, int y, int w, int h)
{
if (msg->x > x && msg->xy>y && msg->y < y + h)
{
return true;
}

return false;

}
*/

int mouseInButton(struct button* pB, MOUSEMSG m)
{
if (pB->x <= m.x && m.x <= pB->x + pB->w && pB->y <= m.y && m.y <= pB->y + pB->h); //这个分号删除。

不知道符合你的需求不?
但这篇博文讲解的很全面,你可看一下【js实现按钮点击变色,其他的按钮恢复默认颜色】,链接:https://blog.csdn.net/weixin_43775400/article/details/126416063

这样发

img

参考链接,跟你这个功能差不多

使用easyx来实现按钮功能_【社会人】的博客-CSDN博客_easyx 按钮 实现思路easyx是实现c/c++的图形库,没有封装好的组件,需要手动完成。思路是画一个长方形,在长方形中水平、垂直居中显示文字。设置文字的位置思路是文字x坐标=按钮边框的起始位置x+(按钮的宽度-文字宽度)/2文字y坐标=按钮边框的起始位置y+(按钮的高度-文字高度)/2按钮构造好后,需要获取鼠标的位置坐标,判断鼠标是否在按钮的宽度、高度的范围内,若在此范围内,进行相应的事件函数。#include <graphics.h> // 引用图形库头文件#include & https://blog.csdn.net/bigorange1/article/details/124695264?ops_request_misc=&request_id=&biz_id=102&utm_term=easyx%E6%8C%89%E9%92%AE%E4%BA%A4%E4%BA%92&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-1-124695264.nonecase&spm=1018.2226.3001.4450

easyX图形库在VS2019中的几个问题及解决
https://blog.csdn.net/weixin_52324181/article/details/119839311

加个焦点Force试试,给三个动作,移入、显示、渐变