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

2023-09-04 03:01:42 作者:可萌可撩

我要注册的MouseEnter /鼠标离开事件被禁止的按钮。它不工作allthough它的工作为启用的按钮。

I want to register MouseEnter/MouseLeave events for disabled buttons. It does not work allthough it does work for enabled buttons..

//Enable Disable controls on form load
                EnableDisableControls("Load");

var grupButtons = control.Controls.OfType<Button>();
                    foreach (Button btns in grupButtons)
                    {
                        //btns.MouseMove += new MouseEventHandler(MainframeDataExchangeTool_MouseMove);
                        btns.MouseEnter += new EventHandler(btns_MouseEnter);
                        btns.MouseLeave += new EventHandler(btns_MouseLeave);
                    }

private void btns_MouseEnter(object sender, EventArgs e)
        {

        }

        private void btns_MouseLeave(object sender, EventArgs e)
        {
            var parent = sender as Control;
            string tipstring = string.Empty;
            if (parent == null)
            {
                return;
            }
            string enter = sender.GetType().ToString() + ": MouseEnter";
        }

这是工作的启动按钮...但要做些什么禁用按钮......我必须表明对的mouseenter提示操作,使之即消失立即在鼠标离开?

It is working for enable button ...but what to do for disable button ... I have to show tooltip operation on mouseenter and make it disapper immediately on mouseleave ?

推荐答案

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

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;
    }
}