我实现了如下的一个Button控件,按照预期,flushStatus仅在上一帧未按下且本帧按下时为 CLICKED。
我通过peekmessage(&mouseMsg, EX_MOUSE)获取鼠标信息并将它传入Button::flushStatus。但是在实际测试中,如果保持鼠标左键按下,在按钮范围内移动鼠标,此时许多次返回了CLICKED。这是为什么?应该如何改进?
//VS2022+Easyx20240601
#pragma once
#include "Utils.h"
class Button {
bool isDown = false;
public:
enum STATUS {
NONE, ON, DOWN, CLICKED
};
RECT area;
inline bool isOn(const ExMessage& mouseMsg) const {
return mouseMsg.x >= area.left && mouseMsg.x <= area.right && mouseMsg.y >= area.top && mouseMsg.y <= area.bottom;
}
inline STATUS flushStatus(const ExMessage& mouseMsg) {
bool last = isDown;
isDown = isOn(mouseMsg) && mouseMsg.lbutton;
if (!isOn(mouseMsg)) return NONE;
if (!isDown) return ON;
if (last) return DOWN;
return CLICKED;
}
};