使用自定义字体绘制标签文本时,参数无效

3

我有一个使用自定义字体和定时器的标签。我的应用程序启动时是最小化的。当我显示应用程序时,有时会显示异常,并在标签中显示红色叉号而不是文本。

在这里,我尝试调用异步方法更改标签文本。

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

    }

这是一种标签文本更改的方法。
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)

问题:我使用自定义字体时如何摆脱这个异常?

2个回答

7
问题在于pfc变量中的PrivateFontCollection实例超出了范围,并且有时在控件第一次绘制之前被回收(之后似乎获取了对该实例的强引用)。
将该实例移到方法外部以防止GC对其进行回收:
class Form1 : Form
{
    readonly PrivateFontCollection _pfc = new PrivateFontCollection();
    public Form1()
    {
        ...

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

        ...
    }
}

有没有更优雅的解决方案? - user3625699

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接