C#中:在一个文本框使用嵌入字体 [英] C# : Using an embedded font on a textbox

查看:763
本文介绍了C#中:在一个文本框使用嵌入字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我嵌入我的应用程序作为EmbeddedResource字体和希望在文本中使用它。
AddMemoryFont帮助,说我必须兼容文本呈现设置为true,使用GDI +等都可以用我的字体,但不知何故,它只是将无法显示正确的字体。

I'm embedding a font in my app as EmbeddedResource and want to use it in a textbox. AddMemoryFont help says I have to set compatible text rendering to true to use GDI+ so my font can be used, but somehow it just won't display the right font.

在Program.cs中我明确状态:
Application.SetCompatibleTextRenderingDefault(真);

in Program.cs I explicitly state : Application.SetCompatibleTextRenderingDefault(true);

那么为什么不工作?
任何人有线索?

So why is it not working ? Anybody got a clue ?

推荐答案

好吧,我理解了它感谢interwebs和谷歌。

Okay, I figured it out thanks to the interwebs and Google.

有关将来参考,如果任何人有这个问题,解决方法是:让你的嵌入字体作为流之后
,并调用AddMemoryFont,
你面前调用AddFontMemResourceEx!
(所以你必须导入它在C#中不可用:

For future reference, if anybody has this problem, the fix is : after getting your embedded font as a stream, and before calling AddMemoryFont, you have to call AddFontMemResourceEx ! (Not available in C# so you have to import it :

    [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它不会工作。

And presto, you'll be able to use the font. Without the AddFontMemResourceEx it wont work.

这篇关于C#中:在一个文本框使用嵌入字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆