我正在制作一个小型的Windows表单应用程序。
在其中我有一个PictureBox(父级)和一个Label(子级)。
父级的鼠标事件完美地工作,但是由子元素生成的鼠标事件没有反映在父级上。光标也会恢复到默认状态(箭头)。
是否可能将由子控件生成的事件(例如MouseEnter事件)传递给父控件?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Card.MouseEnter += new EventHandler(Card_MouseEnter);
Card.MouseLeave += new EventHandler(Card_MouseLeave);
Card.MouseDown += new MouseEventHandler(this.Card_MouseDown);
Card.MouseUp += new MouseEventHandler(this.Card_MouseUp);
}
void Card_MouseLeave(object sender, EventArgs e)
{
this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_bg));
this.Rename("Running!");
}
void Card_MouseEnter(object sender, EventArgs e)
{
this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_hover_bg));
}
private void Card_MouseDown(object sender, EventArgs e)
{
this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_click_bg));
}
private void Card_MouseUp(object sender, EventArgs e)
{
this.Card.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.card_hover_bg));
this.Rename("Please Wait...");
}
private void CardName_MouseDown(object sender, MouseEventArgs e)
{
}
void Rename(string args)
{
this.CardName.Text = args;
}
private void CardName_Click(object sender, EventArgs e)
{
}
}
This is what I have This is what I want to achieve
第一个动画代表我现在拥有的,第二个是我需要实现的 :)
MouseLeave
等事件)。当PictureBox接收到MouseLeave事件时,您可以检查光标位置,以查看它是否仍在PictureBox的范围内(这意味着它进入了Label)。仅当光标实际上在MouseLeave事件中超出PictureBox时,才触发您的自定义“离开”事件。 - Idle_MindCard_MouseEnter(Card, e);
了吗?顺便说一下,当鼠标指针进入PictureBox(或UserControl或其他特定区域)时,你可以简单地绘制该文本并更改光标或执行其他操作。你可以使用Rectangle.Contains(Point)
来实现这一点。 - JimiLoad()
事件中将其设置为父级,以使其保持在相同的位置:(1)Point pt = this.PointToScreen(label1.Location);
(2)pictureBox1.Controls.Add(label1);
(3)label1.Location = pictureBox1.PointToClient(pt);
。 - Idle_MindCardName.Location = new Point(0, Card.Height - CardName.Height); CardName.BringToFront();
(或者Idle_Mind发布的内容),重定位标签,因为其位置现在相对于新的父级。通常最好也将子控件BringToFront()
,因为PictureBox不是ContainerControl。 - Jimi