控件中的多个MouseHover事件

Joã*_*tos 7 c# winforms

我正在尝试在C#中实现自定义控件,我需要在鼠标悬停时获取事件.我知道有MouseHover事件,但它只触发一次.要让它再次触发,我需要使用控件的鼠标并再次输入.

有什么方法可以做到这一点吗?

lc.*_*lc. 8

让我们将"停止移动"定义为"保持在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)


Mas*_*ess 6

我意识到这是一个古老的话题,但我想分享一种我发现的方法:在鼠标移动事件上,在捕获事件的控件上调用ResetMouseEventArgs().