2

I created a simple WinForm Form and tested a custom font file.
Basically I just add several chinese characters (which do not exist) to a existing font file.

Strange thing is that the font only works in WinForm controls such as TextBox, Grid..etc. but does not work when I use Paint event of the Form or Picturebox.

enter image description here

As You can see in the picture, the first character showed perfectly in the TextBox but not showed when I use the DrawString() in the Form Paint event.
Of course, every other ordinary characters are displaying perfectly.

Code:

Private custfont As Font
Private drawBrush As New SolidBrush(Color.Black)
Private chinese_string As String = "英世"

Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim count As Integer = 0
    Dim familyname As String = ""
    Dim fontFamilies() As FontFamily
    Dim private_fontCollection As New PrivateFontCollection

    private_fontCollection.AddFontFile("e:\UNI_HSR_EXT42.TTF")
    fontFamilies = private_fontCollection.Families

    count = fontFamilies.Length

    familyname = fontFamilies(0).Name
    Dim ff As FontFamily = New FontFamily(familyname, private_fontCollection)
    custfont = New Font(ff, 20, FontStyle.Regular)
    Me.TextBox1.Font = custfont
    Me.TextBox1.Text = chinese_string
End Sub

Private Sub frmTest_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    e.Graphics.DrawString(chinese_string, custfont, drawBrush, 50, 50)

End Sub
  • It is not very obvious why the snippet has two DrawString() calls and exactly what you did with that font. TextBox does not use DrawString(), it uses TextRenderer.DrawText(). Something else you can try. – Hans Passant Aug 07 '18 at 16:39
  • Also, don't create your custom Font inside the `Paint()` event. Create it in the Form contructor or `Form Load()`. Note that you assigned the custom Font to the `TextBox` control only, not to the Form. – Jimi Aug 07 '18 at 16:50
  • Thanks for your comment Hans Passant, the first drawstring() call was just a meaningless test. so I remove the lines and tidy up the code a little bit. anyhow it does not work yet. "Custom font works for TextRenderer.DrawText but not for drawstring" could be the same question according to your comment. – YongSeung Kim Aug 07 '18 at 23:26
  • May be form_load is called later then form_paint, try renaming to form_init or assign font file during it’s declaration as stated by Jimi.. – boateng Aug 07 '18 at 23:35
  • Thanks jimi for editing my question! :) I moved the code(creating custom font) to the outside of event. I modified the code in my question a littie bit following your option but still does not work. – YongSeung Kim Aug 07 '18 at 23:40
  • thanks numbtongue but it does not work even I moved the code to form_init or even add a startup module to the project and moved the code blocks to it. – YongSeung Kim Aug 07 '18 at 23:54
  • Use [`TextRenderer.DrawText()`](https://msdn.microsoft.com/en-us/library/4ftkekek(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1). The `IDeviceContext` parameter is the `Paint()` event `e.Graphics` object. Assign beforehand the Font to your Form, too. – Jimi Aug 08 '18 at 00:37
  • When you setting the string on 3rd line I can see the first character is not recognized and displayed as a box, may be font issue missing this character or need to set Culture, and save file a Unicode https://stackoverflow.com/a/93622/3254405 – boateng Aug 08 '18 at 01:11
  • Also, read [this](https://stackoverflow.com/questions/51608365/how-can-label-control-display-japanese-characters-properly-when-font-of-the-labe?answertab=active#tab-top) about what's happening. – Jimi Aug 08 '18 at 01:58
  • 2
    Thank you very much Jimi!!!!! TextRenderer.DrawText works like a charm:) I really really appreciate your help. – YongSeung Kim Aug 10 '18 at 04:39
  • also thanks numbtongue for the kind comments~ – YongSeung Kim Aug 10 '18 at 04:40

0 Answers0