当控件不可见时,MouseHover 和 MouseLeave 不起作用
由于某种原因,MouseHover 和 MouseLeave 函数的行为非常奇怪。我需要做的就是,当光标位于“按钮”上时,我想让按钮可见,当光标离开按钮时,我想让它不可见。无论我尝试什么,我都无法使其发挥作用。当控制对象不可见时,鼠标事件似乎不起作用。
private void button1_MouseHover(object sender, EventArgs e)
{
button1.Visible = true;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Visible = false;
}
For some reason MouseHover and MouseLeave functions behave really strange. All I need to do is, when the cursor is over the "button", I want to make the button visible and when the cursor leaves the button, I want to make it invisible. Whatever I try I couldn't make it work. It seems like Mouse events not working when the control object is invisible.
private void button1_MouseHover(object sender, EventArgs e)
{
button1.Visible = true;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.Visible = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这就是它的工作原理。一种选择是继续以相同的方式处理按钮的
MouseLeave
事件,并为其父级处理MouseMove
事件(我假设为以下形式):That’s how it works. One option is to continue handling the button's
MouseLeave
event the same way, and handleMouseMove
for its parent (I assume the form):将按钮放置在尺寸和位置恰好包含该按钮的
Panel
上。然后在面板上挂接MouseEnter
和MouseLeave
。显示/隐藏按钮;保持面板始终可见,以便它可以获取鼠标事件。Put the button onto a
Panel
that's sized and positioned to exactly contain the button. Then hookMouseEnter
andMouseLeave
on the panel. Show/hide the button; leave the panel always visible so it can get the mouse events.正如简洁的评论所暗示的那样,鼠标无法识别不可见的物体,因为它们“不存在”。
As the terse comment suggests, invisible objects are not recognized by the mouse, because they "aren't there".
这就是它的工作原理;不可见控件不响应鼠标事件。
重新考虑一下你的设计怎么样?仅当鼠标滚动到其上时才会出现的隐形控件只会尖叫“很难使用”。我可以理解当鼠标悬停在父容器或控件上时会出现一些子控件,但不是一个单独的按钮,在纯粹靠运气找到之前是不可见的。您始终可以将按钮包装在另一个容器中并处理该容器的鼠标事件。
That's how it works; invisible controls do not respond to mouse events.
How about reconsidering your design? An invisible control that only appears when the mouse scrolls over it just screams "hard to use". I could understand a few child controls appearing when hovering over a parent container or control, but not a lone button, invisible until found purely by luck. You could always just wrap the button in another container and handle the container's mouse events.