儿童控制的火灾事件事件

时间:2013-09-30 07:54:25

标签: c# .net winforms events

我有一个名为panel1的Panel。 panel1有一个“mosuseHover”事件处理程序.panel1也有一些控件,如pictureBox,label等。

当我在panel1上移动鼠标时,事件正确触发,但当鼠标courser进入panel1控件时,如pictureBox,事件无效。 如何在鼠标控件上使用鼠标控件时调用事件。

我应该注意,我不想为每个子控件创建事件处理程序。

最好的问候

3 个答案:

答案 0 :(得分:4)

您可以添加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;
    }
}

注意:上述代码适用于{strong} Panel 中任何级别的嵌套控件。如果您的面板仅包含停止在级别1的子控件。您可以使用c = Control.FromChildHandle(m.Hwnd)稍微更改代码并按c==panel1检查控件的父级,而无需使用HasParent方法检查为了孩子 - 祖先的关系。

答案 1 :(得分:1)

您可以在子项上创建一个事件处理程序,只需使用相同的参数调用面板处理程序即可。 另请查看this thread

答案 2 :(得分:0)

我反驳了同样的问题。我通过创建一个MouseEnter事件来解决它,并在其中声明控制ctl =发送者为控件;然后我调用了ctl的Parent,其中,控制面板= ctl.Parent; 现在做任何你想做的事情,如 panel.BackColor = Color.Red;

像这样:

private void label_MouseEnter(object sender, System.EventArgs e)
    {
        Control ctl = sender as Control; // gets the label control
        Control panel = ctl.Parent; // gets the label's parent control, (Panel in this case)
        if (ctl != null)
        {
            ctl.Font = new Font(ctl.Font.Name, 11, FontStyle.Underline); // change the font of the Label and style...
            panel.BackColor = Color.Blue; // change the panel's backColor
            ctl.Cursor = Cursors.Hand;
            // do more with the panel & label 
        }
    }

但是不要忘记遍历所有控件并获取Panel以及Panel内的任何内容。 像这样:

        public YourApp()
        {
            InitializeComponent();
            foreach (Control objCtrl in this.Controls) // get all the controls in the form
            {

                if (objCtrl is Panel) // get the Panel
                {
                    objCtrl.MouseEnter += new EventHandler(panel_MouseEnter); // do something to the panel on MouseEnter
                    objCtrl.MouseLeave += new EventHandler(panel_MouseLeave);
                    foreach (Control ctl in objCtrl.Controls) // get all the controls inside the Panel
                    {
                        if (ctl is Label) // get the label inside the panel
                        {
                            ctl.MouseEnter += new System.EventHandler(label_MouseEnter);
                            ctl.MouseLeave += new EventHandler(label_MouseLeave);
                            ctl.Click += new EventHandler(label_Click);
                        }
                        if (ctl is PictureBox) // get the pictureBox inside the panel
                        {
                            ctl.MouseEnter += new EventHandler(label_MouseEnter);
                        }
                    }
                }
            }
        }

你可以弄清楚其余部分。 希望这会有所帮助。