您可以像这样添加一个IMessageFilter
来实现您自己global MouseHover
的Panel
:
//This code uses some reflection so you must add using System.Reflection
public partial class Form1 : Form, IMessageFilter
{
public Form1(){
InitializeComponent();
Application.AddMessageFilter(this);
}
public bool PreFilterMessage(ref Message m) {
Control c = Control.FromHandle(m.HWnd)
if (HasParent(c,panel1)){
if (m.Msg == 0x2a1){//WM_MOUSEHOVER = 0x2a1
//Fire the MouseHover event via Reflection
typeof(Panel).GetMethod("OnMouseHover", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(panel1, new object[] {EventArgs.Empty});
}
}
return false;
}
//This method is used to check if a child control has some control as parent
private bool HasParent(Control child, Control parent) {
if (child == null) return false;
Control p = child.Parent;
while (p != null) {
if (p == parent) return true;
p = p.Parent;
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
注意:上面的代码被实现为适用于Panel
. 如果您的面板仅包含停止在级别 1 的子控件。您可以通过使用c = Control.FromChildHandle(m.Hwnd)
和检查控件的父级来稍微更改代码,c==panel1
而不必使用HasParent
检查子-祖先关系的方法。