举报

鼠标移动时lbutton不稳定

0

我实现了如下的一个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;
	}
};
ava
Joehan

2024-11-6

举报
0
	bool last = isDown;	// false
	isDown = isOn(mouseMsg) && mouseMsg.lbutton;	// true
	if (!isOn(mouseMsg)) return NONE;	// false
	if (!isDown) return ON;				// false
	if (last) return DOWN;				// false

没看错的话,三个 if 条件都是 false,最后总返回 CLICKED 呗

ava
xiongfj ◑◑

2024-11-7

哦我明白了,应该写if (!last)判断上一帧没有按下,是我逻辑出问题了,谢谢。 -  Joehan  2024-11-7
举报
技术讨论社区