我正在尝试在 C# 中实现自定义控件,我需要在鼠标悬停时获取事件。我知道有 MouseHover 事件,但它只触发一次。要让它再次触发,我需要使用控件的鼠标并再次输入它。
有什么办法可以做到这一点吗?
最佳答案
让我们将“停止移动”定义为“在 n
毫秒内保持在 x
像素半径内”。
订阅 MouseMove 事件并使用计时器(设置为 n
毫秒)设置超时。每次鼠标移动时,检查公差。如果它超出了您的容忍度,请重置计时器并记录一个新的起点。
伪代码:
Point lastPoint;
const float tolerance = 5.0;
//you might want to replace this with event subscribe/unsubscribe instead
bool listening = false;
void OnMouseOver()
{
lastpoint = Mouse.Location;
timer.Start();
listening = true; //listen to MouseMove events
}
void OnMouseLeave()
{
timer.Stop();
listening = false; //stop listening
}
void OnMouseMove()
{
if(listening)
{
if(Math.abs(Mouse.Location - lastPoint) > tolerance)
{
//mouse moved beyond tolerance - reset timer
timer.Reset();
lastPoint = Mouse.Location;
}
}
}
void timer_Tick(object sender, EventArgs e)
{
//mouse "stopped moving"
}
关于c# - 控件中的多个 MouseHover 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/809922/