简体   繁体   English

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

[英]C# : Using an embedded font on a textbox

I'm embedding a font in my windows forms application as embedded resource, and want to use it in a TextBox .我在我的 windows 窗体应用程序中嵌入了一种字体作为嵌入资源,并希望在TextBox使用它。

The help of AddMemoryFont() says I have to set compatible text rendering to true in order to use GDI+, and so my font can then be used. AddMemoryFont()的帮助说明我必须将兼容的文本呈现设置为true才能使用 GDI+,然后才能使用我的字体。 But somehow it just won't display the right font.但不知何故,它只是不会显示正确的字体。

In Program.cs I explicitly state:在 Program.cs 中,我明确指出:

Application.SetCompatibleTextRenderingDefault(true);

So why is it not working?那么为什么它不起作用呢? Anybody got a clue on how to set a custom font to a TextBox?有人知道如何为 TextBox 设置自定义字体吗?

Okay, I figured it out thanks to the interwebs and Google.好的,多亏了互联网和谷歌,我才知道。

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 !为了将来参考,如果有人遇到这个问题,修复方法是:在将嵌入的字体作为流获取之后,在调用 AddMemoryFont 之前,您必须调用 AddFontMemResourceEx ! (Not available in C# so you have to import it : (在 C# 中不可用,因此您必须导入它:

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

and then :进而 :

            //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);

And presto, you'll be able to use the font.而且很快,您将能够使用该字体。 Without the AddFontMemResourceEx it wont work.如果没有 AddFontMemResourceEx,它将无法工作。

Thanks it's working.谢谢它的工作。 TO Embed Font in c# Windows Application在 c# Windows 应用程序中嵌入字体

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

    PrivateFontCollection pFC = new PrivateFontCollection();

        try
        {
            string[] resource = { "newFont-Bold.ttf", "newFont-Regular.ttf" }; // specify embedded resource name

            foreach (var item in resource)
            {
                // receive resource stream
                Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(item);

                // create an unsafe memory block for the font data
                System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);

                // create a buffer to read in to
                byte[] fontdata = new byte[fontStream.Length];

                // read the font data 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);

                ///IMPORTANT line to register font in system
                uint cFonts = 0;
                AddFontMemResourceEx(data, (uint)fontdata.Length, IntPtr.Zero, ref cFonts);

                // pass the font to the font collection
                pFC.AddMemoryFont(data, (int)fontStream.Length);

                // close the resource stream
                fontStream.Close();
                // free up the unsafe memory
                Marshal.FreeCoTaskMem(data);
            }
        }
        catch (Exception exp)
        {
            Log.Error(exp);
        }

        return pFC;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM