2012-08-06 72 views
0

我使用自定義字體和定時器標籤中的值發生變化的標籤。我的應用程序開始最小化。當我顯示應用程序時,有時會顯示異常並且標籤中的文字是紅色的。使用自定義字體在標籤中繪製文本時參數無效

這裏我嘗試調用異步方法用於標籤文本變化

private void timer1_Tick(object sender, EventArgs e) 
    { 
     // create a delegate of MethodInvoker poiting to showTime function. 
     MethodInvoker simpleDelegate = new MethodInvoker(showTime); 
     // Calling showTime Async 
     simpleDelegate.BeginInvoke(null, null); 
    } 

字體加載

public Form1() 
    { 
     InitializeComponent(); 

     SystemEvents.SessionSwitch += new SessionSwitchEventHandler(SystemEvents_SessionSwitch); //event handler for windows lock 

     File.WriteAllBytes(appPath + "\\font.ttf", Resources.font); //copy font from resources 

     try 
     { 
      PrivateFontCollection pfc = new PrivateFontCollection(); 
      pfc.AddFontFile(appPath + @"/font.ttf"); 
      label1.Font = new Font(pfc.Families[0], 11, FontStyle.Bold); 
     } 
     catch 
     { 
      MessageBox.Show("Failed to load nice font." + "\r\n" + "Using standart font instead.", "Time app", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
     } 

    } 

這裏是標籤TET變化的方法

private void showTime() 
    { 
     label1.Text = time.ToString(); 
    } 

** * ** 異常文本 ** * ****

System.ArgumentException: Parameter is not valid. 
at System.Drawing.FontFamily.GetName(Int32 language) 
at System.Drawing.FontFamily.get_Name() 
at System.Windows.Forms.Internal.WindowsFont.FromFont(Font font, WindowsFontQuality fontQuality) 
at System.Windows.Forms.Internal.WindowsGraphicsCacheManager.GetWindowsFont(Font font, WindowsFontQuality fontQuality) 
at System.Windows.Forms.TextRenderer.MeasureText(String text, Font font, Size proposedSize, TextFormatFlags flags) 
at System.Windows.Forms.Layout.LayoutUtils.MeasureTextCache.GetUnconstrainedSize(String text, Font font, TextFormatFlags flags) 
at System.Windows.Forms.Layout.LayoutUtils.MeasureTextCache.TextRequiresWordBreak(String text, Font font, Size size, TextFormatFlags flags) 
at System.Windows.Forms.Label.CreateTextFormatFlags(Size constrainingSize) 
at System.Windows.Forms.Label.CreateTextFormatFlags() 
at System.Windows.Forms.Label.OnPaint(PaintEventArgs e) 
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer) 
at System.Windows.Forms.Control.WmPaint(Message& m) 
at System.Windows.Forms.Control.WndProc(Message& m) 
at System.Windows.Forms.Label.WndProc(Message& m) 
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

問題:如何在使用自定義的字體林擺脫這種異常?

回答

0

下面是在這裏的另一個問題中使用的示例。它顯示你需要什麼。 http://www.bobpowell.net/embedfonts.htm

+0

404:

移動實例的方法外,以防止從GC收集它。也許類似的信息可以在http://dotnet-coding-helpercs.blogspot.com/2009/06/how-to-embed-true-type-font-in-using-c.html? – erdomke 2013-05-23 19:02:46

0

的問題是,在pfc變量PrivateFontCollection實例超出範圍,並且有時被吸入首次(這似乎獲得的強引用實例之後)控制之前被收集。鏈接

class Form1 : Form 
{ 
    readonly PrivateFontCollection _pfc = new PrivateFontCollection(); 
    public Form1() 
    { 
     ... 

     _pfc.AddFontFile(appPath + @"/font.ttf"); 

     ... 
    } 
} 
相關問題