c# - 控件中的多个 MouseHover 事件

标签 c# winforms

我正在尝试在 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/

相关文章:

C# - 创建字典<T, 管理器<T>>

c# - 使用 IIS 的通用处理程序解析错误

c# - GeoCoordinate.GetDistanceTo 使用错误的地球半径?

c# - 生成错误 : "The process cannot access the file because it is being used by another process"

c# - javascript :void(0) called 时 WebBrowser 控件未完成加载

c# - 需要对我解决此 Winforms 竞争条件的第二(和第三)意见

c# - 选择 nchar 或 nvarchar 之类的位置

c# - 如何从 C# 程序在 ASP.NET 页面上填写文本?

c# - Windows 窗体转换 Button 类

c# - 如何在 c#/winforms 中将图像裁剪成圆形?