FontAwesome-For-WinForms-CSharp で FontAwesome を使うと 「使用されたパラメーターが有効ではありません。」
C# の Windows Form プロジェクトで FontAwesome-For-WinForms-CSharp
を使っているとデバッグ開始時に、しばしば (毎回ではない) 例外が発生する。
原因は不明だが回避できた (ている、今のところ) ので記録しておく。以下は、情報の正しさと効果をまったく保証しないので利用者の責任において使用すること。
使用したライブラリ
FontAwesome-For-WinForms-CSharp
Thx.
エラー情報
エラーメッセージ
使用されたパラメーターが有効ではありません。
例外発生時のスタックトレースはこんなかんじ。
場所 System.Drawing.FontFamily.GetName(Int32 language)
場所 System.Drawing.FontFamily.get_Name()
場所 System.Windows.Forms.Internal.WindowsFont.FromFont(Font font, WindowsFontQuality fontQuality)
場所 System.Windows.Forms.Internal.WindowsGraphicsCacheManager.GetWindowsFont(Font font, WindowsFontQuality fontQuality)
場所 System.Windows.Forms.TextRenderer.MeasureText(String text, Font font, Size proposedSize, TextFormatFlags flags)
場所 System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.LayoutOptions.GetTextSize(Size proposedSize)
場所 System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.LayoutOptions.LayoutTextAndImage(LayoutData layout)
場所 System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.LayoutOptions.Layout()
場所 System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintWorker(PaintEventArgs e, Boolean up, CheckState state)
場所 System.Windows.Forms.ButtonInternal.ButtonStandardAdapter.PaintUp(PaintEventArgs e, CheckState state)
場所 System.Windows.Forms.ButtonInternal.CheckBoxStandardAdapter.PaintUp(PaintEventArgs e, CheckState state)
場所 System.Windows.Forms.ButtonInternal.ButtonBaseAdapter.Paint(PaintEventArgs pevent)
場所 System.Windows.Forms.ButtonBase.OnPaint(PaintEventArgs pevent)
場所 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
場所 System.Windows.Forms.Control.WmPaint(Message& m)
場所 System.Windows.Forms.Control.WndProc(Message& m)
場所 System.Windows.Forms.ButtonBase.WndProc(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
場所 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
場所 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
回避方法
public static void Reload() {
PrivateFontCollection f=new PrivateFontCollection();
f.AddFontFile( FontAwesomeTTF );
FontAwesome=new Font( f.Families[0], Size, Style );
}
上記の PrivateFontCollection
のインスタンスをクラスのフィールドに移動する。誰が、いつ呼ぶのかわからないが。
static class Fonts {
private static PrivateFontCollection _f=new PrivateFontCollection();
public static string FontAwesomeTTF { get; set; }
public static Font FontAwesome { get; set; }
:
public static void Reload() {
if ( _f != null ) {
_f.Dispose();
}
_f=new PrivateFontCollection();
_f.AddFontFile( FontAwesomeTTF );
FontAwesome=new Font( _fonts.Families[0], Size, Style );
}
}
確認した環境
- OS: 日本語 Windows 10 Home 64 ビット (1607)
- コンパイラ: Visual Studio Community 2015 Update 3