C# 在父级的 MouseHover/MouseLeave 上显示/隐藏元素

发布于 2024-08-14 09:18:46 字数 647 浏览 5 评论 0原文

在 C# 中,我们有以下内容:

  • 一个 UserControl,其中包含一个 PictureBox 和一个不可见的 FlowPanel

我想要实现的目标:

  • UserControl 悬停时 (MouseHover),不可见的 FlowPanel 将设置为 visible = 正确。当鼠标离开时 UserControl FlowPanelFlowPanel 应设置 visible = 假

UserControl 上使用 MouseLeave 并不能完成这项工作,因为当鼠标进入 FlowPanel 时会触发此事件。当鼠标离开FlowPanel时隐藏FlowPanel可以做到这一点,但是有问题(有时MouseLeave被触发,有时不会)。

解决这个问题的最佳方法是什么?

In C#, we have the following:

  • A UserControl containing a PictureBox and an invisible FlowPanel.

What I want to achieve:

  • When the UserControl is hovered
    (MouseHover), the invisible
    FlowPanel will be set to visible =
    true
    . When the mouse leaves the
    UserControl or FlowPanel, the
    FlowPanel should be set visible =
    false
    .

Using MouseLeave on UserControl doesn't do the job, because this event is triggered when the mouse enters FlowPanel. Hiding the FlowPanel when the mouse leaves FlowPanel does it, but is buggy (sometimes MouseLeave is triggered, sometimes not).

What's the best way to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

格子衫的從容 2024-08-21 09:18:46

我在我的一个表单上做了类似的事情,

在第一个事件中执行 if(contorl.Opacity = 1.0)

private void Form1_MouseLeave(object sender, EventArgs e)
{
   if (this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
   {
    this.Opacity = 1.0;
   }
   else
   {
      int loopctr = 0;

      for (loopctr = 100; loopctr >= 5; loopctr -= 10)
      {
        this.Opacity = loopctr / 99.0;
        this.Refresh();
        Thread.Sleep(100);
      }
   }
}

i did somthing similar on one of my forms

do a if(contorl.Opacity = 1.0) inside your first event

private void Form1_MouseLeave(object sender, EventArgs e)
{
   if (this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
   {
    this.Opacity = 1.0;
   }
   else
   {
      int loopctr = 0;

      for (loopctr = 100; loopctr >= 5; loopctr -= 10)
      {
        this.Opacity = loopctr / 99.0;
        this.Refresh();
        Thread.Sleep(100);
      }
   }
}
ˉ厌 2024-08-21 09:18:46

FlowPanel.MouseLeave没有触发的情况下,UserControl.MouseLeave不是也被触发了吗?我认为隐藏这两个事件可能会奏效。

In the case when FlowPanel.MouseLeave isn't triggered, isn't UserControl.MouseLeave triggered? I suppose that hiding on both events may do the trick.

梦断已成空 2024-08-21 09:18:46

这是一个常见的 UI 问题。鼠标事件作为样本出现,因此可能会丢失某些像素位置,并且控件无法获取鼠标释放事件。

一种不太好的工作方式是,当在 Control 内检测到 MouseHover 时设置某种形式的计时器,并定期轮询光标(例如 342 毫秒)。

This is a common UI problem. Mouse events come up as samples so it's possible that some pixel positions are missed and a control doesn't get the mouse up event.

A not so nice way that works is setting up some form of Timer when MouseHover is detected inside the Control and poll for the cursor in regular intervals (such as 342ms).

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