简体   繁体   English

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

[英]C# Show/hide element on MouseHover/MouseLeave of the parent

In C#, we have the following: 在C#中,我们具有以下内容:

  • A UserControl containing a PictureBox and an invisible FlowPanel . UserControl包含PictureBox和一个不可见的FlowPanel

What I want to achieve: 我要实现的目标:

  • When the UserControl is hovered ( MouseHover ), the invisible FlowPanel will be set to visible = true . UserControl悬停( MouseHover )时,不可见的FlowPanel将设置为visible = true When the mouse leaves the UserControl or FlowPanel , the FlowPanel should be set visible = false . 当鼠标离开UserControl FlowPanelFlowPanel设置为visible = false

Using MouseLeave on UserControl doesn't do the job, because this event is triggered when the mouse enters FlowPanel . UserControl上使用MouseLeave不会执行此操作,因为当鼠标进入FlowPanel时会触发此事件。 Hiding the FlowPanel when the mouse leaves FlowPanel does it, but is buggy (sometimes MouseLeave is triggered, sometimes not). 当鼠标离开FlowPanel时,将FlowPanel隐藏FlowPanel是可以做到的,但它有问题(有时会触发MouseLeave ,有时不会触发)。

What's the best way to fix this? 解决此问题的最佳方法是什么?

i did somthing similar on one of my forms 我在其中一种表格上做了类似的事情

do a if(contorl.Opacity = 1.0) inside your first event 在您的第一个事件中执行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);
      }
   }
}

In the case when FlowPanel.MouseLeave isn't triggered, isn't UserControl.MouseLeave triggered? 在未触发FlowPanel.MouseLeave的情况下,是否不会触发UserControl.MouseLeave I suppose that hiding on both events may do the trick. 我想躲在两个事件上都可以解决问题。

This is a common UI problem. 这是一个常见的UI问题。 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). 一种不太好用的方法是在控件内检测到MouseHover时设置某种形式的Timer,并以固定的时间间隔(例如342ms)轮询光标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM