2012-01-25 35 views
1

我創建了透明標籤控件,透明效果很好。但是,當我更新控件的文本字段時,我發現原始文本在繪製新文本之前未清除。所以如果我多次更改控件的文本字段,它很快就變得不可讀。透明標籤控件無法正常刷新

任何線索?謝謝!

public partial class TransLabel : Label 
{ 
    public TransLabel() 
    { 
     InitializeComponent(); 

     this.SetStyle(ControlStyles.Opaque, true); 
     this.SetStyle(ControlStyles.OptimizedDoubleBuffer, false); 
     this.Font = new Font("Franklin Gothic Book", 12f, FontStyle.Regular); 
     this.ForeColor = Color.White; 
     this.BackColor = Color.Transparent; 
    } 

    public override string Text 
    { 
     get 
     { 
      return base.Text; 
     } 
     set 
     { 
      base.Text = value; 
      this.Invalidate(); // seems to have no effect 
      this.Refresh(); // seems to have no effect 
     } 
    } 

    protected override void OnPaintBackground(PaintEventArgs pevent) 
    { 
     //do nothing 
    } 

    protected override void OnMove(EventArgs e) 
    { 
     RecreateHandle(); 
    } 

    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle = 0x00000020; //WS_EX_TRANSPARENT 
      return cp; 
     } 
    } 
} 
+0

我想你可以調用窗體上的'.Refresh()'方法嗎?我不知道...... – psyklopz

+0

看起來很熟悉...... –

+0

如果內存服務,您需要擦除'OnPaintBackground'事件中標籤的內容。我知道我在創建自定義控件之前遇到過這個問題,儘管我的透明度不夠高,所以我不能100%確定。 – CodingGorilla

回答

1

試着改變你的Text屬性如下:

public override string Text { 
    get { 
    return base.Text; 
    } 
    set { 
    base.Text = value; 
    if (this.Parent != null) 
     this.Parent.Invalidate(this.Bounds, false);   
    } 
} 

由於WinForms的不具有透明度真正的支持,我認爲你必須父容器失效。

另外,當繼承控件時,通常沒有InitializeComponent()方法。

+0

工作就像一個魅力!謝謝! –

+0

上面的控件只有在不重疊定期重繪自己的另一個控件時才能正常工作,如進度條。該標籤將消失,並在進度條重繪時開始閃現。 – Legends