【发布时间】:2013-01-04 03:06:21
【问题描述】:
我目前有一个具有完全透明背景的表单。目前,当用户将鼠标悬停在表单上的控件上时,我必须绘制出现在论坛顶部的框。
将鼠标悬停在 PictureBox 上会正确触发 MouseEnter 事件并将按钮 Visible 状态设置为 true,MouseLeave 事件将其设置为 false。按钮本身具有相同的MouseEnter 和MouseLeave 事件,但是当我去时,Winforms 将鼠标事件传递到表单上透明的任何空间下的表单(我在按钮中使用的图像在它们周围也是透明的)单击按钮,它们会消失,因为表单认为鼠标已经“离开”了按钮或表单。有谁知道阻止事件传递的任何方法?
你问一些代码?你得到一些代码:)
// Form Constructor!
// map = picturebox, this = form, move = first button, attach = second button
public Detached(PictureBox map)
{
InitializeComponent();
doEvents(map, this, this.attach, this.move);
}
// doEvents method! I use this to add the event to all controls
// on the form!
void doEvents(params Control[] itm)
{
Control[] ctls = this.Controls.Cast<Control>().Union(itm).ToArray();
foreach (Control ctl in ctls)
{
ctl.MouseEnter += (s, o) =>
{
this.attach.Visible = true;
this.move.Visible = true;
};
ctl.MouseLeave += (s, o) =>
{
this.attach.Visible = false;
this.move.Visible = false;
};
}
}
【问题讨论】:
-
我们可以看看一些 EventHandler 代码吗:)
-
@sa_ddam213 如你所愿:D
-
@HansPassant 这实际上是让表单变为透明的问题,我的问题是试图让表单在鼠标进入透明区域时引发事件。
-
@HansPassant 对不起,我欠你一个道歉。我只看了问题而不是你的答案。这可以工作,但不是最佳的。
标签: c# winforms events transparency