Apply parent mouse events to child elements

Tif*_*any 5 c# parent custom-controls mouseevent winforms

I am making little Windows Forms Application.
\nI have PictureBox (parent) and Label (child) in it.

\n

The Parent\'s Mouse Events are working perfectly, but Mouse events generated by child elements are not reflected on the Parent. The Cursor also changes back to its default (arrow).

\n

Is it possible to pass events generated by child Controls, e.g., the MouseEnter event, to the Parent Control?

\n
public partial class Form1 : Form\n{\n    public Form1()\n    {\n        InitializeComponent();\n\n        Card.MouseEnter += new EventHandler(Card_MouseEnter);\n        Card.MouseLeave += new EventHandler(Card_MouseLeave);\n        Card.MouseDown += new MouseEventHandler(this.Card_MouseDown);\n        Card.MouseUp += new MouseEventHandler(this.Card_MouseUp);\n    }\n\n    void Card_MouseLeave(object sender, EventArgs e)\n    {\n        this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_bg));\n        this.Rename("Running!");\n    }\n\n    void Card_MouseEnter(object sender, EventArgs e)\n    {\n        this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_hover_bg));\n    }\n\n    private void Card_MouseDown(object sender, EventArgs e)\n    {\n        this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_click_bg));\n    }\n\n    private void Card_MouseUp(object sender, EventArgs e)\n    {\n        this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_hover_bg));\n        this.Rename("Please Wait...");\n    }\n\n    private void CardName_MouseDown(object sender, MouseEventArgs e)\n    {\n        \n    }\n\n    void Rename(string args)\n    {\n        this.CardName.Text = args;\n    }\n\n    private void CardName_Click(object sender, EventArgs e)\n    {\n\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

在此输入图像描述

\n

设计器中的元素

\n

\xc2\xa0\xc2\xa0这就是我所拥有的 \xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0 这就是我想要实现的目标

\n
            This is what I have                       This is what I want to achieve\n    \n
Run Code Online (Sandbox Code Playgroud)\n

The first animation represents what I have now, the second is what I need to achieve :)

\n

Idl*_*ind 2

当我制作 pictureBox1.Controls.Add(label1) label1 时,label1 正在消失,我尝试将其置于前面并更改颜色,但无法做到这一点。如果您有任何想法,请向我展示我提供的代码,以便我可以理解。再次感谢大家:)

您可能会在表单的 Load() 事件中使用这样的代码:

private void Form1_Load(object sender, EventArgs e)
{
    Point pt = CardName.Parent.PointToScreen(CardName.Location);
    Card.Controls.Add(CardName);
    CardName.Location = Card.PointToClient(pt);
}
Run Code Online (Sandbox Code Playgroud)

这使标签保持在与原来相同的位置,但使图片框成为父级。

不知道你哪里出错了。这是一个展示其实际效果的示例。PictureBox (Card) 和 Label (CardName) 均位于面板 (panel1) 内。单击按钮 2 可切换卡片的可见性。单击button1 使Card 成为CardName 的父级。您可以看到,一开始,只有 Card 切换可见性,但在单击 Button1 并设置 Parent 后,两者一起切换可见性,因为 CardName 是 Card 的子级(它还会更改其 BackColor 以匹配其新 Parent 的 BackColor):

在此输入图像描述

代码:

public partial class Form1 : Form
{

    private void button1_Click(object sender, EventArgs e)
    {
        Point pt = CardName.Parent.PointToScreen(CardName.Location);
        Card.Controls.Add(CardName);
        CardName.Location = Card.PointToClient(pt);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Card.Visible = !Card.Visible;
    }

}
Run Code Online (Sandbox Code Playgroud)

当我将鼠标移到标签上时,面板认为鼠标离开了它并引发 MouseLeave 事件

下面是如何判断光标是否确实离开了面板的边界,而不是简单地在面板中输入子控件:

private void panel1_MouseEnter(object sender, EventArgs e)
{
    panel1.BackColor = Color.Red;
}

private void panel1_MouseLeave(object sender, EventArgs e)
{
    Point pt = panel1.PointToClient(Cursor.Position);
    if (!panel1.ClientRectangle.Contains(pt))
    {
        // we only get in here when the cursor leaves the BOUNDS of panel1
        panel1.BackColor = Control.DefaultBackColor;
    }            
}
Run Code Online (Sandbox Code Playgroud)