代码之家  ›  专栏  ›  技术社区  ›  Led

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

  •  8
  • Led  · 技术社区  · 16 年前

    我在windows窗体应用程序中嵌入一个字体作为嵌入资源,并希望在 TextBox .

    在…的帮助下 AddMemoryFont() 说我必须将兼容文本呈现设置为 true

    在Program.cs中,我明确指出:

    Application.SetCompatibleTextRenderingDefault(true);
    

    那为什么它不起作用呢?有人知道如何设置文本框的自定义字体吗?

    1 回复  |  直到 5 年前
        1
  •  28
  •   Led    16 年前

    好吧,多亏了互联网和谷歌我才弄明白。

    为便于将来参考,如果有人遇到此问题,解决方法是: 将嵌入字体作为流获取后,在调用AddMemoryFont之前, 你必须打电话给AddFontMemResourceEx!

        [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,它将无法工作。

        2
  •  1
  •   PPr    6 年前

    在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;