2009-12-23 7 views
8

Incrustaré una fuente en mi aplicación como EmbeddedResource y quiero usarla en un cuadro de texto. La ayuda de AddMemoryFont dice que tengo que establecer la representación de texto compatible en verdadero para usar GDI +, así que se puede usar mi fuente, pero de alguna manera simplemente no se mostrará la fuente correcta.C#: Usar una fuente incrustada en un cuadro de texto

en Program.cs declaro explícitamente: Application.SetCompatibleTextRenderingDefault (true);

¿Por qué no funciona? ¿Alguien tiene una pista?

Respuesta

19

Bien, lo descubrí gracias a las interwebs y a Google.

Para referencia futura, si alguien tiene este problema, la solución es: después de conseguir su fuente incrustada como una corriente, y antes de llamar AddMemoryFont, usted tiene que llamar AddFontMemResourceEx! (No disponible en C# por lo que tiene que importarlo:

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

y luego:.

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

Y listo, usted será capaz de utilizar la fuente Sin la AddFontMemResourceEx que no funcionará

+0

+1 útil saber que, gracias, Led – BillW

+0

Holy cangrejo He estado golpeando mi cabeza contra la pared durante horas gracias! – Mike

+0

¿Dónde está "fontStream" viniendo desde aquí? –

Cuestiones relacionadas