m0_74195216 2022-11-16 09:37 采纳率: 62.5%
浏览 121
已结题

easyx按钮交互出现bug

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

img

img


源代码在这
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<easyx.h>
#include<graphics.h>
#include<mmsystem.h>
#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->x<x + w && msg->y>y && msg->y < y + h)
{
return true;
}

return false;

}
*/

  • 写回答

6条回答 默认 最新

  • 浪客 2022-11-16 11:21
    关注

    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); //这个分号删除。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 11月27日
  • 已采纳回答 11月19日
  • 创建了问题 11月16日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果