2009-12-23 8 views
8

Sto incorporando un carattere nella mia app come EmbeddedResource e voglio usarlo in una casella di testo. AddMemoryFont help dice che devo impostare il rendering di testo compatibile su true per usare GDI + in modo che il mio font possa essere usato, ma in qualche modo semplicemente non mostrerà il font giusto.C#: utilizzo di un carattere incorporato su una casella di testo

in Program.cs, dichiaro esplicitamente: Application.SetCompatibleTextRenderingDefault (true);

Quindi perché non funziona? Qualcuno ha un indizio?

risposta

19

Ok, l'ho capito grazie all'interwebs e Google.

Per riferimento futuro, se qualcuno ha questo problema, la correzione è: dopo aver ottenuto il carattere incorporato come un flusso, e prima di chiamare AddMemoryFont, devi chiamare AddFontMemResourceEx! (non disponibile in C# quindi bisogna importarlo:

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

e poi:.

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

E presto, sarete in grado di utilizzare il tipo di carattere Senza l'AddFontMemResourceEx non funzionerà .

+0

+1 utile sapere che, grazie, Led – BillW

+0

Santo granchio ho stato sbattere la testa contro il muro per ore grazie !! – Mike

+0

Dove si trova il "fontStream" proveniente da qui? –