一个控件中的多个 MouseHover 事件 [英] Multiple MouseHover events in a Control
问题描述
我正在尝试在 C# 中实现自定义控件,我需要在鼠标悬停时获取事件.我知道有 MouseHover 事件,但它只触发一次.要让它再次触发,我需要拿起控件的鼠标并再次输入.
I'm trying to implement a custom control in C# and I need to get events when the mouse is hovered. I know there is the MouseHover event but it only fires once. To get it to fire again I need to take the mouse of the control and enter it again.
有什么办法可以做到这一点吗?
Is there any way I can accomplish this?
推荐答案
让我们将停止移动"定义为在 n
毫秒内保持在 x
像素半径内".
Let's define "stops moving" as "remains within an x
pixel radius for n
ms".
订阅 MouseMove 事件并使用计时器(设置为 n
ms)来设置超时.每次鼠标移动时,检查公差.如果超出您的容忍范围,请重置计时器并记录新的原点.
Subscribe to the MouseMove event and use a timer (set to n
ms) to set your timeout. Each time the mouse moves, check against the tolerance. If it's outside your tolerance, reset the timer and record a new origin.
伪代码:
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"
}
这篇关于一个控件中的多个 MouseHover 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!