2013-01-21 82 views
0

我目前有一个完全透明的背景。目前,当用户将鼠标悬停在表单上的某个控件上时,必须在论坛顶部显示出图框。透明度和鼠标事件WinForms

Screenshot!

悬停在图片框正确触发MouseEnter事件并设置按钮Visible状态设置为true和MouseLeave事件其设置为false。这些按钮本身具有相同的MouseEnterMouseLeave事件,但由于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; 
     }; 
    } 
} 
+0

我们可以看到一些事件处理代码:) –

+0

@ sa_ddam213如你所愿:d – jduncanator

+0

[MouseHover和鼠标离开事件控制(可能重复http://stackoverflow.com/questions/12552809/mousehover-and-mouseleave-events-controlling) –

回答

0

感谢Hans Passant指引我正确的方向。我最终创建了一个线程,用于检查鼠标是否每隔50ms处于边界内。

public Detached(PictureBox map) 
{ 
    Thread HoverCheck = new Thread(() => 
    { 
     while (true) 
     { 
      if (this.Bounds.Contains(Cursor.Position)) 
      { 
       ToggleButtons(true); 
      } 
      else 
      { 
       ToggleButtons(false); 
      } 
      Thread.Sleep(50); 
     } 
    }); 
    HoverCheck.Start(); 
} 

void ToggleButtons(bool enable) 
{ 
    if (InvokeRequired) 
    { 
     Invoke(new MethodInvoker(() => ToggleButtons(enable))); 
     return; 
    } 

    this.attach.Visible = enable; 
    this.move.Visible = enable; 
    this.pictureBox1.Visible = enable; 
} 

谢谢:)