我正在尝试在C#中实现自定义控件,我需要在鼠标悬停时获取事件.我知道有MouseHover事件,但它只触发一次.要让它再次触发,我需要使用控件的鼠标并再次输入.
有什么方法可以做到这一点吗?
让我们将"停止移动"定义为"保持在ms 的x像素半径n内".
订阅MouseMove事件并使用计时器(设置为nms)来设置超时.每次鼠标移动时,请检查公差.如果超出容差范围,请重置计时器并记录新的原点.
伪代码:
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"
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4435 次 |
| 最近记录: |