gpt4 book ai didi

c# - 如果鼠标进入 C# ListView 的滚动条则显示工具提示

转载 作者:太空宇宙 更新时间:2023-11-03 13:30:39 25 4
gpt4 key购买 nike

我想在鼠标光标进入 System.Windows.Forms.ListView 的垂直滚动条时立即显示工具提示。对于 DataGridView,这很容易,因为有对滚动条的引用,我可以为其处理 MouseEnter/MouseLeave 事件。

当鼠标悬停在滚动条上时,甚至不会触发 ListView 的 MouseMove 事件。这个问题有解决办法吗?

最佳答案

当鼠标指针进入或离开正在“监视”的控件时,ControlWatcher 类会引发事件。它允许您捕获多个控件的鼠标事件,或停止捕获这些事件。

public partial class Form1 : Form
{
public ToolTip toolTip { get; set; }

public Form1()
{
InitializeComponent();
toolTip = new ToolTip();

var watcher = new ControlWatcher();
watcher.MouseEnter += watcher_MouseEnter;
watcher.MouseLeave += watcher_MouseLeave;
watcher.AddControl(listView1);
watcher.StartListening();
}

void watcher_MouseEnter(Control control)
{
var pt = control.PointToClient(Cursor.Position);
toolTip.Show("Super tooltip", control, pt);
}

void watcher_MouseLeave(Control control)
{
toolTip.Hide(control);

}
}

public class ControlWatcher : IMessageFilter
{
public event Action<Control> MouseEnter;
public event Action<Control> MouseLeave;

private Control lastControl;

private List<Control> controls;

public ControlWatcher()
{
controls = new List<Control>();
}

public void StartListening()
{
Application.AddMessageFilter(this);
}

public void StopListening()
{
Application.RemoveMessageFilter(this);
}

public void AddControl(Control c)
{
controls.Add(c);
}

private bool IsMouseOverControl(Control control)
{
var pt = control.PointToClient(Cursor.Position);
var isOver = (pt.X >= 0 && pt.Y >= 0 && pt.X <= control.Width && pt.Y <= control.Height);
return isOver;
}

private void OnMouseEnter(Control control)
{
if (MouseEnter != null)
MouseEnter(control);
}

private void OnMouseLeave(Control control)
{
if (MouseLeave != null)
MouseLeave(control);
}

public bool PreFilterMessage(ref Message m)
{
if (m.Msg != 675 && m.Msg!=512)
{
return false;
}
//the control under the cursor
var actControl = controls.FirstOrDefault(IsMouseOverControl);

if (lastControl != null && actControl == null)
{
OnMouseLeave(lastControl);
lastControl = null;
return false;
}

if (lastControl==null && actControl != null && actControl != lastControl)
{
lastControl = actControl;
OnMouseEnter(actControl);
return false;
}
return false;
}
}

关于c# - 如果鼠标进入 C# ListView 的滚动条则显示工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20678863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com