2009-12-23 3 views
8

내 응용 프로그램에 EmbeddedResource로 글꼴을 포함시키고 텍스트 상자에서 사용하려고합니다. AddMemoryFont 도움말은 내 글꼴을 사용할 수 있도록 GDI +를 사용하려면 호환되는 텍스트 렌더링을 true로 설정해야하지만 어떻게 든 올바른 글꼴을 표시하지 않는다고 말합니다.C# : 텍스트 상자에 포함 된 글꼴 사용

in Program.cs 명시 적으로 상태 : Application.SetCompatibleTextRenderingDefault (true);

그렇다면 왜 작동하지 않습니까? 아무도 단서가 있습니까?

답변

19

좋아, 나는 interwebs와 Google 덕택으로 그것을 이해했다. 사람이 문제가있는 경우 나중에 참조 할 수 있도록

은, 수정은 다음과 같습니다 스트림로 포함 된 글꼴을 받고 후, 그리고 AddMemoryFont, 당신이 AddFontMemResourceEx 전화를해야 를 호출하기 전에! 다음

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

과 :. 당신이 그것을 가져 그래서 C#에서 사용할 수 없습니다 (

  //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없이 실 거예요 작업

+0

+1 덕분에 감사합니다. Led – BillW

+0

거룩한 게 몇 시간 동안 벽에 머리를 두드 리고있었습니다! – Mike

+0

여기에서 "fontStream"이 어디입니까? –