2013-01-21 114 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; 
} 

謝謝:)