自制按钮-鼠标悬停变色功能失败

0

开发环境:Microsoft Visual Studio 2019

操作系统:Wihdows 10

我想实现一个有鼠标悬停变色的按钮功能。因为是新手,所以没有使用相关函数,故写了一个函数。

可是,本代码编译后出现了如下问题:

  1. 背景颜色应设置为白色,实际则为黑色
  2. 鼠标悬停不变色
  3. 点击了按钮,不会返回点击消息

请问应该如何处理?

#include <iostream>
#include <graphics.h>
#include <conio.h>
#include <Windows.h>
using namespace std;
int printbutton(COLORREF button_color,COLORREF hovering_color,int left, int top, int right, int bottom, int ellipsewidth, int ellipseheight)
{
    setfillcolor(button_color);
    fillroundrect(left, top, right, bottom, ellipsewidth, ellipseheight);
    while (1) 
    {
        MOUSEMSG m;
        m = GetMouseMsg();
        if (m.x >= left && m.x <= right && m.y >= top && m.y <= bottom)
        {
            setfillcolor(hovering_color);
            floodfill(left + 1, top + 1, button_color, 1);
            if (WM_LBUTTONUP == 1)
            {
                break;
                return 1;
            }
        }
        else
        {
            setfillcolor(button_color);
            floodfill(left + 1, top + 1, hovering_color, 1);
        }
    }
    return 0;
}
int main()
{
    initgraph(800, 800, SHOWCONSOLE);
    setbkcolor(WHITE);
    if (printbutton(GREEN, RED, 144, 24, 641, 115, 5, 5) == 1)
    {
        //todo
        cout << "成功click" << endl;
        FlushMouseMsgBuffer();
    }
    _getch();
    closegraph();
}
ava
小俊逸

2020-3-12

2

背景色设置完后需要调用 cleardevice()  才会生效。

floodfill(left + 1, top + 1, button_color, 1);  中的 +1 改为 +2

if (WM_LBUTTONUP == 1)  中的 1 改为 m.uMsg

具体可参考文档:https://docs.easyx.cn/zh-cn/MOUSEMSG

ava
xiongfj ◑◑

2020-3-12

技术讨论社区