当控件不可见时,MouseHover 和 MouseLeave 不起作用

发布于 2024-11-17 17:49:17 字数 344 浏览 10 评论 0原文

由于某种原因,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝眸 2024-11-24 17:49:17

这就是它的工作原理。一种选择是继续以相同的方式处理按钮的 MouseLeave 事件,并为其父级处理 MouseMove 事件(我假设为以下形式):

private void Form_MouseMove(object sender, MouseEventArgs e) {
    if (button1.Bounds.Contains(e.Location) && !button1.Visible) {
        button1.Show();
    }
}

That’s how it works. One option is to continue handling the button's MouseLeave event the same way, and handle MouseMove for its parent (I assume the form):

private void Form_MouseMove(object sender, MouseEventArgs e) {
    if (button1.Bounds.Contains(e.Location) && !button1.Visible) {
        button1.Show();
    }
}
时光礼记 2024-11-24 17:49:17

将按钮放置在尺寸和位置恰好包含该按钮的 Panel 上。然后在面板上挂接MouseEnterMouseLeave。显示/隐藏按钮;保持面板始终可见,以便它可以获取鼠标事件。

Put the button onto a Panel that's sized and positioned to exactly contain the button. Then hook MouseEnter and MouseLeave on the panel. Show/hide the button; leave the panel always visible so it can get the mouse events.

暗藏城府 2024-11-24 17:49:17

正如简洁的评论所暗示的那样,鼠标无法识别不可见的物体,因为它们“不存在”。

As the terse comment suggests, invisible objects are not recognized by the mouse, because they "aren't there".

遮云壑 2024-11-24 17:49:17

这就是它的工作原理;不可见控件不响应鼠标事件。

重新考虑一下你的设计怎么样?仅当鼠标滚动到其上时才会出现的隐形控件只会尖叫“很难使用”。我可以理解当鼠标悬停在父容器或控件上时会出现一些子控件,但不是一个单独的按钮,在纯粹靠运气找到之前是不可见的。您始终可以将按钮包装在另一个容器中并处理该容器的鼠标事件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文