2011-01-26 28 views
0

我希望我的板是真正透明的,所以我也跟着這篇文章上的說明: http://www.bobpowell.net/transcontrols.htm透明面板不會呈現滾動條正確

不過,我迫使我的面板始終顯示垂直滾動條。它最初不會被渲染,除非我將鼠標光標懸停在它上面,然後開始出現。除了將VerticalScroll.Visible設置爲true以外,還可以在上面的文章中添加更多內容以確保我的面板的滾動條始終可見?

這是我到目前爲止對我的自定義面板類。這是在Visual Studio 2010中使用C#.NET 4.0:

public class SkinnedList : Panel 
{ 
    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT 
      return cp; 
     } 
    } 

    public SkinnedList() 
    { 
     AdjustFormScrollbars(true); 
    } 

    public new void AdjustFormScrollbars(bool visible) 
    { 
     VerticalScroll.Visible = true; 
     HorizontalScroll.Visible = false; 
    } 

    protected override void OnPaint(PaintEventArgs e) 
    { 
     base.OnPaint(e); 
    } 

    protected override void OnPaintBackground(PaintEventArgs e) 
    { 
     // Do not render background 
    } 

    protected void InvalidateEx() 
    { 
     if(Parent != null) 
     { 
      Rectangle rc = new Rectangle(Location, Size); 
      Parent.Invalidate(rc, true); 
     } 
    } 
} 

回答

1

InvalidateEx()方法不正確,您需要將矩形從面板座標映射到父座標。就像這樣:

protected void InvalidateEx() { 
    if (Parent != null) { 
     Rectangle rc = new Rectangle(0, 0, this.ClientSize.Width - SystemInformation.VerticalScrollBarWidth, this.ClientSize.Height); 
     rc = this.RectangleToScreen(rc); 
     rc = Parent.RectangleToClient(rc); 
     Parent.Invalidate(rc, false); 
    } 
} 

獲得滾動條的最好方法是使用AutoScrollMinSize屬性還:

public SkinnedList() { 
    this.AutoScroll = true; 
    this.AutoScrollMinSize = new Size(0, 1000); 
    this.Scroll += delegate { this.InvalidateEx(); }; 
} 

這應該可以解決你的問題,只有一個除外。您會注意到「拖動時顯示窗口內容」系統選項的效果。它最好形容爲'做pogo'。沒有解決這個問題,你不能合理地關閉系統選項。這是行不通的。

1

這就是您選擇的窗口樣式。該控件是透明的,意味着如果不活躍,它應該融入到背景中,並且只在需要時才進行交互。

一些建議,我不知道是否有任何人會爲你的情況下工作:

  • 巢您的透明面板正常的面板,並有透明的自動調整大小本身,以適應其內容。然後,滾動條將位於包含面板上,並將滾動自動化的透明面板。這使滾動條始終可見,但可能會破壞透明效果。

  • 把你的透明面板,在具有導航按鈕(上,下,PageUp鍵,PageDown鍵,等等),將觸發透明面板上的事件(Scroll是大個),一個用戶控件。這將要求你的透明面板有按鈕點擊事件的處理程序,在這個事件中它將調用它自己的OnScroll()方法。這不會看起來像一個標準的滾動條,你將無法點擊並拖動(除非你使用滑塊),但你可以輕鬆解決。