将PrivateFontCollection添加到RichTextBox

时间:2012-12-14 08:29:21

标签: c# fonts privatefontcollection

我正在尝试将嵌入式资源字体更改为我的RichTextBox。 但它没有用。它在标签上工作正常,但在我的richtextbox上没有。 在将字体更改为richtextbox之后,我在控制台中写出了我的richtextbox的字体名称,并且它拼出了正确的名称,但它没有更改屏幕上的字体。

这是我的代码

//define a private font collection
System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
//read your resource font into a byte array
byte[] Bytes = Properties.Resources.BRAILLE11;
//allocate some memory and get a pointer to it
IntPtr ptr = System.Runtime.InteropServices.Marshal.AllocCoTaskMem(Bytes.Length);
//copy the font data byte array to memory
System.Runtime.InteropServices.Marshal.Copy(Bytes, 0, ptr, Bytes.Length);
//Add the font to the private font collection
pfc.AddMemoryFont(ptr, Bytes.Length);
//free up the previously allocated memory
System.Runtime.InteropServices.Marshal.FreeCoTaskMem(ptr);
//define a font from the private font collection
System.Drawing.Font fnt = new System.Drawing.Font(pfc.Families[0], 16f, System.Drawing.FontStyle.Regular, GraphicsUnit.Point);
//dispose of the private font collection
pfc.Dispose();

//return the font created from your font resource
richTextBoxEditor.Font = fnt;
lblStatus.Font = fnt;

Console.Write(richTextBoxEditor.Font.Name);

fnt.Dispose();

2 个答案:

答案 0 :(得分:0)

Properties.Resources.BRAILLE11一个.bin文件还是.ttf文件?

查看以下链接:

  1. Embedding a font into an application
  2. Rhymes with Amharic (a.k.a. How about a little breakfast embed, dear?)

答案 1 :(得分:0)

我认为富文本框可以在一个TextBox中包含多个字体的文本,如Bold,Italics等。所以你必须选择所有Text First。 看到。 http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionfont.aspx