2013-06-19 11 views
0

我必須在我的TableLayoutPanel上禁用OnPaintBackground以消除由於首先繪製背景而導致的閃爍(因爲我使用paint方法繪製TLP,並且是的我需要TLP,因爲它包含許多用於某種目的的控制)。所以我的代碼如下:覆蓋OnPaintBackground使設計器視圖出現異常

public static bool FlickerPanel = false; 
    public class FlickerTableLayoutPanel : TableLayoutPanel 
    { 
     protected override void OnPaintBackground(PaintEventArgs e) 
     { 
      if (FlickerPanel) 
       base.OnPaintBackground(e); 
     } 
    } 

然後在我的繪畫方法中,我畫了它自己的背景。所以在運行時它很好。

編輯:我發現了問題的根源。通過重寫OnPaintBackground,可以禁用任何讓設計者繪製背景的代碼。如果我一起刪除覆蓋它沒有圖形故障。

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

即使上面的代碼禁用了設計視圖渲染並導致圖形故障。任何幫助非常感謝!

+0

你不能只跳過OnPaintBackground使用,這也正是吸引TLP本身。在基礎調用之後添加e.Graphics方法調用您想要的方法。如果這仍然太明顯,那麼添加一個構造函數並將DoubleBuffered屬性設置爲true。 –

+0

經過一番研究,我認爲Hans Passant建議的DoubleBuffered屬性可以做到這一點。 –

+0

@HansPassant太棒了! DoubleBuffered屬性確實有效。所以我把'public TLP(){base.DoubleBuffered = true; }'它的工作!這與重寫默認構造函數是一樣的嗎?我應該在這裏添加原始默認構造函數嗎? – CodeCamper

回答

4

我也有問題,檢測自己的狀態是否處於設計模式。我解決了它如下:

首先,我不得不寫一個IsDesignMode()方法:

public static bool IsDesignMode(Control control) 
{ 
    if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) // Initial quick check. 
    { 
     return true; 
    } 

    while (control != null) 
    { 
     System.ComponentModel.ISite site = control.Site; 

     if ((site != null) && (site.DesignMode)) 
      return true; 

     control = control.Parent; 
    } 

    return false; 
} 

我把這種方法在一個共享庫組件(在下面的例子中的命名空間「Windows.Forms.Utilities」) ,因爲我在很多項目中使用它。

然後對我需要知道,如果它在設計模式下,每個用戶或自定義的控制,我要補充一個private bool _isDesignMode領域和覆蓋OnHandleCreated()如下:

protected override void OnHandleCreated(EventArgs e) 
{ 
    base.OnHandleCreated(e); 
    _isDesignMode = Windows.Forms.Utilities.IsDesignMode(this); 
} 

那麼我可以用_isDesignMode無論我需要至。

+0

不錯的代碼,絕對會將這個加入到武器庫中。可悲的是我發現,即使我一起刪除了「if」,問題也會發生。只要重寫OnPaintBackground,即使將它傳遞給原始基礎,它仍會產生相同的圖形故障。 – CodeCamper

+0

據我所知,每個控件都有一個名爲'DesignMode'的受保護屬性,爲什麼不直接使用它? –

+0

@KingKing因爲嵌套控件無法正常工作。 [見這裏舉例](http://stackoverflow.com/questions/34664/designmode-with-controls/708594#708594) –

1

你可以圍繞在OnPaintBackground方法的代碼與if語句來檢測,如果你的設計時間是這樣的:

if (System.ComponentModel.LicenseManager.UsageMode == 
System.ComponentModel.LicenseUsageMode.Designtime) 
+0

這是很酷的代碼。可悲的是,在嘗試這個美妙的代碼後,我發現這種行爲無論如何都發生了 – CodeCamper

+0

@CodeCamper這可能是因爲該測試並不總是適用於嵌套控件。 –

+0

@MatthewWatson,但我刪除了'if'全部檢查,只是像我在我的問題的底部一樣賤基類,它仍然無法工作。我甚至不知道從哪裏開始解決這個問題,因爲我以前從未爲設計人員編寫過代碼。 – CodeCamper

0

什麼都不做onPaintBackground()功能。它防止backgroundImage被繪製並且應該修正閃爍。

protected override void OnPaintBackground(PaintEventArgs e) 
{ 
} 
1

我只是想貢獻一下我對matthew-watson提供的解決方案的輕微升級。作爲擴展方法,語法變得更加令人愉快。

public static partial class ExtensionMethods 
{ 
    public static bool IsInDesignMode(this Control source) 
    { 
     var result = LicenseManager.UsageMode == LicenseUsageMode.Designtime; 

     while (result == false && source != null) 
     { 
      result = source.Site != null && source.Site.DesignMode; 

      source = source.Parent; 
     } 

     return result; 
    } 
} 

像這樣

protected override void OnPaintBackground(PaintEventArgs pevent) 
{ 
    if (this.IsInDesignMode()) 
    { 
     base.OnPaintBackground(pevent); 
    } 
}