试图实现一个鼠标移动到按钮上按钮就变色的程序,但是我封装好的按钮最开始不会显示,鼠标一移到界面内就四个按钮一起出现然后变色
源代码在这
#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;
}
*/