2017-02-16 7 views
-1

enter image description hereWinフォームが埋め込まれたリソースのフォントはコントロールに正常に動作していないフォント埋め込みリソース

で正常に動作しません。

しかし、これはGraphicsオブジェクト描画のもので使用できます。

  1. まず


    、埋め込みリソースとfontawesome-webfont.ttfフォントファイルを追加します。

  2. 次に、アセンブリリソースからフォントを取得します。

  3. 次に、コントロールにfontプロパティを割り当てます。ここ

コード:これは便利です

using System; 
using System.Drawing; 
using System.Drawing.Drawing2D; 
using System.Drawing.Text; 
using System.IO; 
using System.Reflection; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 

namespace Sample 
{ 
    public partial class FormSample : Form 
    { 
     PrivateFontCollection pfc; 
     Font font ; 

     public FormSample() 
     { 
      InitializeComponent(); 
      pfc = LoadFontFromResource("Sample.assets.fontawesome-webfont.ttf"); 
      font = new Font(pfc.Families[0], 16, FontStyle.Regular); 
      this.textBoxControl1.Text = "\uf028 fontawesome"; 
      this.label1.Text = "\uf028 fontawesome"; 
      this.label1.ForeColor = Color.Green; 
      this.textBoxControl1.ForeColor = Color.Green; 
      this.label1.Font = font; 
      this.textBoxControl1.Font = font; 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      this.label1.Font = font; 
      this.textBoxControl1.Font = font; 

      Graphics g = this.CreateGraphics(); 

      g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; 
      g.InterpolationMode = InterpolationMode.HighQualityBilinear; 
      g.PixelOffsetMode = PixelOffsetMode.HighQuality; 
      g.SmoothingMode = SmoothingMode.HighQuality; 

      g.DrawString("\uf028 fontawesome(GDI+)", font, new SolidBrush(Color.Green), new Point(this.label1.Location.X+10, 80)); 
      g.Dispose(); 
     } 

     /// <summary> 
     /// Loads the font from resource. 
     /// </summary> 
     /// <param name="name">The name.</param> 
     /// <returns>PrivateFontCollection.</returns> 
     public static PrivateFontCollection LoadFontFromResource(string name) 
     { 
      PrivateFontCollection pfc = new PrivateFontCollection(); 

      Assembly assembly = Assembly.GetExecutingAssembly(); 

      using (Stream stream = assembly.GetManifestResourceStream(name)) 
      { 
       if (stream == null) return null; 

       byte[] fontData = new byte[stream.Length]; 
       stream.Read(fontData, 0, (int)stream.Length); 

       IntPtr ptr = Marshal.AllocHGlobal(fontData.Length); 
       Marshal.Copy(fontData, 0, ptr, fontData.Length); 

       pfc.AddMemoryFont(ptr, fontData.Length); 

       return pfc; 
      } 
     } 
    } 
} 
+0

このフォームに関連するコードを投稿できますか?何が正しく動作していないのかを正確に説明してください。 –

答えて

関連する問題