esayx按钮功能实现不了

为什么按钮功能实现不了啊,按钮点不动

#include <graphics.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>

struct Button {
    int x;
    int y;
    int w;
    int h;
    COLORREF curColor;    //按钮当前显示的颜色
    COLORREF inColor;    //鼠标在按钮里面的颜色
    COLORREF outColor;    //鼠标不在按钮中的颜色
    char* text;
};

//1.创建按钮
struct Button* createButton(int x, int y, int w, int h, const char* str,
    COLORREF inColor, COLORREF outColor)
{
    struct Button* pB = (struct Button*)malloc(sizeof(struct Button));
    assert(pB);
    pB->x = x;
    pB->y = y;
    pB->w = w;
    pB->h = h;
    pB->inColor = inColor;
    pB->outColor = outColor;
    pB->curColor = pB->outColor;
    int textLength = strlen(str) + 1;
    pB->text = (char*)malloc(sizeof(char) * textLength);
    assert(pB->text);
    strcpy_s(pB->text, textLength, str);
    return pB;
}

//2.画按钮
void drawButton(struct Button* pB)
{
    //1.按钮就是一个矩形,画一个矩形
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 2);
    setfillcolor(pB->curColor);
    fillroundrect(pB->x, pB->y, pB->x + pB->w, pB->y + pB->h, 10, 10);
    //2.画文字
    settextcolor(BLACK);    //设置文字颜色
    setbkmode(TRANSPARENT);    //去掉文字背景
    settextstyle(15, 0, "楷体");
    //文字如何居中
    //textwidth(str),textheigh(str);
    int textw = textwidth(pB->text);
    int texth = textheight(pB->text);
    int xx = pB->x + (pB->w - textw) / 2;
    int yy = pB->y + (pB->h - texth) / 2;
    outtextxy(xx, yy, pB->text);
}

//3.鼠标是否在按钮中
bool isInButton(struct Button* pB, ExMessage m) {
    if (m.x > pB->x && m.x<pB->x + pB->w && m.y>pB->y && m.y < pB->y + pB->h) {
        pB->curColor = pB->inColor;
        return true;
    }
    pB->curColor = pB->outColor;
    return false;
}

//4.鼠标点击
bool isClickButton(struct Button* pB, ExMessage m) {
    if (isInButton(pB, m) && m.message == WM_LBUTTONDOWN) {
        return true;
    }
    return false;
}

//5.画屏幕
void drawScreen() {
    
    setbkcolor(WHITE);
    cleardevice();
    setlinecolor(BLACK);
    setlinestyle(PS_SOLID, 2);
    line(240, 50, 360, 50);
    setfillcolor(RGB(193, 210, 240));
    line(300, 50, 300, 150);
    fillcircle(300, 150, 50);


    line(250, 150, 250, 300);
    line(350, 150, 350, 300);

    fillrectangle(240, 300, 260, 320);
    fillrectangle(340, 300, 360, 320);


    settextstyle(30, 0, "微软雅黑");
    settextcolor(BLACK);
    outtextxy(280, 500, "作者:");
    FlushBatchDraw();
}

//6.开始运行
void beginRun() {

    int t = 0;
    int l = 0;
    int y1 = 300;
    int y2 = 300;


    while (y1 - 300 <= 150) {
        setbkcolor(WHITE);
        cleardevice();
        setlinecolor(BLACK);
        setlinestyle(PS_SOLID, 2);
        line(240, 50, 360, 50);
        setfillcolor(RGB(193, 210, 240));
        line(300, 50, 300, 150);
        fillcircle(300, 150, 50);


        line(250, 150, 250, y1);
        line(350, 150, 350, y2);

        fillrectangle(240, y1, 260, y1 + 20);
        fillrectangle(340, y2, 360, y2 + 20);

        t += 1;
        l = (2 * t * t) / 2;
        y1 += l;
        y2 -= l;

        settextstyle(30, 0, "微软雅黑");
        settextcolor(BLACK);
        outtextxy(280, 500, "作者:");
        FlushBatchDraw();
        Sleep(150);
    }

}
int main() {
    initgraph(600, 600);
    struct Button* begin = createButton(20, 20, 80, 30, "begin", RGB(193, 210, 240), RGB(204, 213, 240));
    struct Button* reset = createButton(20, 80, 80, 30, "reset", RGB(193, 210, 240), RGB(204, 213, 240));
    ExMessage m;

    BeginBatchDraw();
    drawScreen();
    drawButton(begin);
    drawButton(reset);
    peekmessage(&m, EM_MOUSE);
    
    if (isClickButton(begin, m)) {
        beginRun();
    }

    if (isClickButton(reset, m)) {
        cleardevice();
        drawScreen();
    }

    EndBatchDraw();
    getchar();
    closegraph();

    return 0;
}

  • 以下回答由chatgpt基于相关博客总结生成:

    由于提供的参考资料与问题没有任何关联,我无法给出任何解决方案。请提供与问题相关的参考资料,以便我能够合理地回答该问题。