C#:在文本框中使用嵌入字体

我在我的应用程序中嵌入了一个字体作为EmbeddedResource,并希望在文本框中使用它。 AddMemoryFont帮助说我必须将兼容的文本呈现设置为true才能使用GDI +,这样我的字体就可以使用,但不知怎的,它只是不会显示正确的字体。

在Program.cs中我明确说明:Application.SetCompatibleTextRenderingDefault(true);

那么为什么它不起作用? 有人知道吗?

好吧,我想通过互联网和谷歌来解决这个问题。

为了将来参考,如果有人遇到这个问题,修复方法是:将嵌入字体作为流后,在调用AddMemoryFont之前,你必须调用AddFontMemResourceEx! (在C#中不可用,因此您必须导入它:

[DllImport("gdi32.dll")] private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts); 

然后 :

  //create an unsafe memory block for the data System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length); //create a buffer to read in to Byte[] fontData = new Byte[fontStream.Length]; //fetch the font program from the resource fontStream.Read(fontData, 0, (int)fontStream.Length); //copy the bytes to the unsafe memory block Marshal.Copy(fontData, 0, data, (int)fontStream.Length); // We HAVE to do this to register the font to the system (Weird .NET bug !) uint cFonts = 0; AddFontMemResourceEx(data, (uint)fontData.Length, IntPtr.Zero, ref cFonts); //pass the font to the font collection mFontCollection.AddMemoryFont(data, (int)fontStream.Length); //close the resource stream fontStream.Close(); //free the unsafe memory Marshal.FreeCoTaskMem(data); 

并且,您将能够使用该字体。 没有AddFontMemResourceEx它就不会工作。