2013-02-21 18 views
0

這很奇怪。當我在按鈕的繪畫事件中執行此操作時:執行繪製事件後按鈕文字消失

using (LinearGradientBrush brush = new LinearGradientBrush(button1.ClientRectangle, 
                   Color.Orange, 
                   Color.Red, 
                   90F)) 
{ 
    e.Graphics.FillRectangle(brush, this.ClientRectangle); 
} 

該按鈕的文本消失。我怎樣才能讓文字回來?

回答

2

您基本上正在繪製控件的頂部。您應該嘗試對按鈕進行子分類並覆蓋OnPaintBackground以在文本後面繪製。

0

爲什麼不設置按鈕的背景畫筆?

+0

據我記得,你不能像使用Windows窗體那樣做。 – Andrey 2013-02-21 14:46:39

+0

好吧,認爲這是WPF對不起 – Benny 2013-02-21 14:50:22

0
  1. 致電button1.Invalidate()。這將重新繪製文本...和整個按鈕。
  2. 以前的建議可能不會產生你想要的。由於您決定自己繪製按鈕,因此您還將負責在背景上繪製文本,請參閱DrawString
0

如果你不想使用的背景屬性或重寫OnPaintBackground方法,你可以做到以下幾點:

//你的背景畫代碼

public void DrawText(Graphics g, Rectangle bounds, string text, Font font, Brush brush) 
{ 
    float x = bounds.Width/2; 
    float y = bounds.Height /2; 

    SizeF textSize = g.MeasureString(text, font); 

    x = (x - (textSize.Width/2) + bounds.X); 
    y = (x - (textSize.Height/2) + bounds.Y); 

    g.DrawString(text, font, brush, new PointF(x, y)); 
} 

,並使用它像所以

DrawText(g, button1.ClientRectangle, button1.Text, button1.Font, new SolidBrush(button1.ForeColor)); 

無的這實際上是雖然SOOOOO yeaaaa測試....

編輯:如果您選擇走這條路線,您將不得不記住,當控件調整大小時,需要重新繪製控件。