2017-10-13 109 views
1

用一個例子比用單詞解釋我的問題更容易。爲什麼TextRenderer沒有在Usercontrol中繪製Wingdings字體?

這是我的UserControl1代碼:

namespace WindowsFormsApplication1 { 

    public partial class UserControl1 : UserControl { 

     public UserControl1() { 
      InitializeComponent(); 
     } 

     private void UserControl1_Paint(object sender, PaintEventArgs e) { 
      Graphics g = CreateGraphics(); 
      TextRenderer.DrawText(g, "à", new Font("Wingdings", 12), new Point(10, 10), Color.Black); 
      TextRenderer.DrawText(g, "à", Font, new Point(30, 10), Color.Black); 
      TextRenderer.DrawText(g, "à", new Font(Font.Name, Font.Size), new Point(50, 10), Color.Black); 
      g.Dispose(); 
     } 
    } 
} 

我這個控件添加到窗體並將其字體屬性設置爲宋體12
第二TextRenderer行不畫一個向右的箭頭,而只是「一個」。
第三行確實繪製了一個箭頭,與第一行完全相同。

也許我錯過了一些設置,有人可以解釋一下嗎?

+2

很難看出問題所在,我沒有看到字體映射程序介入的強制性理由。但是,呃,-100優雅點不會覆蓋OnPaint,不會使用e.Graphics,也不會使用字體。 –

+0

回答爲新答案。 –

回答

2

您在第一個DrawText()函數調用中創建了Wingdings Font對象,但不保存該對象以用於第二個和第三個DrawText()函數調用,因此它們將恢復爲默認字體。具體來說,他們使用的是this.Font對象,它是UserControl的字體集。

若要解決此問題,您需要將Wingdings Font對象保存在變量中,然後在每次調用DrawText()時重新使用它。

此外,您不應該在Paint事件處理函數內調用CreateGraphics()函數。 Paint事件處理程序已獲取一個Graphics對象以作爲PaintEventArgs的成員進行繪製。這樣做效率更高,也更簡單,因爲您不必費心去處置它。

當你需要處理的東西(如Font對象),最好是把它包在using聲明。這確保了將自動處理,而無需擔心自己調用Dispose()或出現異常。

因此,服用這些點考慮,你應該重寫你的代碼:

private void UserControl1_Paint(object sender, PaintEventArgs e) { 
    using (Font f = new Font("Wingdings", 12)) { 
     TextRenderer.DrawText(e.Graphics, "à", f, new Point(10, 10), Color.Black); 
     TextRenderer.DrawText(e.Graphics, "à", f, new Point(30, 10), Color.Black); 
     TextRenderer.DrawText(e.Graphics, "à", f, new Point(50, 10), Color.Black); 
    } 
} 

另外,如果你總是想你的用戶控件繪製文本使用宋體,你可以只設置字體作爲控件的字體:

public partial class UserControl1 : UserControl { 

    public UserControl1() { 
     InitializeComponent(); 
     this.Font = new Font("Wingdings", 12); 
    } 

    private void UserControl1_Paint(object sender, PaintEventArgs e) { 
     TextRenderer.DrawText(e.Graphics, "à", this.Font, new Point(10, 10), Color.Black); 
     TextRenderer.DrawText(e.Graphics, "à", this.Font, new Point(30, 10), Color.Black); 
     TextRenderer.DrawText(e.Graphics, "à", this.Font, new Point(50, 10), Color.Black); 
    } 
} 

我認爲這將是稍微更有效,因爲它並不需要創建一個新的Font對象的每個控制被漆成時間。

+0

感謝您對代碼效率的考慮。然而,第一次調用DrawText()是爲了比較而放置的。我已經將表單設計器中的UserControl Font屬性設置爲Wingdings 12,所以這應該是'this.Font'。實際上,因爲在第三次調用'新的字體(Font.Name,Font.Size)'結果在Wingdings 12,這是正確繪製。 –

-1

我已經改變了我的代碼爲「優雅」的格式:

namespace WindowsFormsApplication1 { 
public partial class UserControl1 : UserControl { 

    public UserControl1() { 
     InitializeComponent(); 
    } 

    protected override void OnPaint(PaintEventArgs e) { 
     base.OnPaint(e); 
     using (Font f1 = new Font("Wingdings", 12)) 
     using (Font f2 = new Font(Font.Name, Font.Size)) { 
      TextRenderer.DrawText(e.Graphics, "à", f1, new Point(10, 10), Color.Black); 
      TextRenderer.DrawText(e.Graphics, "à", Font, new Point(30, 10), Color.Black); 
      TextRenderer.DrawText(e.Graphics, "à", f2, new Point(50, 10), Color.Black); 
     } 
    } 
} 
} 

這使得在結果絕對沒有任何變化!
在第一個和第三個調用DrawText()繪製一個箭頭,在第二個調用中繪製「à」。

相關問題