且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

注册的MouseEnter /鼠标离开事件,在Windows窗体禁用的控件?

更新时间:2023-12-06 11:37:34

您可以尝试一些表范围内的鼠标消息这样的解决方案:

You can try some Form-wide Mouse message solution like this:

//Suppose your disabled Button is button1
public partial class Form1 : Form, IMessageFilter
{
    public Form1()
    {
        InitializeComponent();
        button1.Enabled = false;
        button1.BackColor = Color.Green;
        //Try this to see it in action
        button1.MouseEnter += (s, e) => {
            button1.BackColor = Color.Red;
        };
        button1.MouseLeave += (s, e) => {
            button1.BackColor = Color.Green;
        };
        Application.AddMessageFilter(this);//Add the IMessageFilter to the current Application
    }
    bool entered;
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 0x200) //WM_MOUSEMOVE = 0x200
        {
            if (Control.FromHandle(m.HWnd) == button1.Parent && 
                button1.ClientRectangle.Contains(button1.PointToClient(MousePosition)))
            {
                if (!entered) {
                    entered = true;
                    //Raise the MouseEnter event via Reflection
                    typeof(Button).GetMethod("OnMouseEnter", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                        .Invoke(button1, new[] { EventArgs.Empty });
                }                    
            }
            else if (entered) {
                //Raise the MouseLeave event via Reflection
                typeof(Button).GetMethod("OnMouseLeave", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                    .Invoke(button1, new []{EventArgs.Empty});
                entered = false;                    
            }
        }
        return false;
    }
}