3

I try Defining a Font Fallback Sequence in Code , i refer at here

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        FontFamily f = new FontFamily("Comic Sans MS, Verdana");
    }
}

but i have i error: "Additional information: Font 'Comic Sans MS, Verdana' cannot be found."

how to Defining a Font Fallback Sequence in Code.

Petter Hesselberg
  • 5,062
  • 2
  • 24
  • 42

3 Answers3

1

Because "Comic Sans MS, Verdana" is no font name. You can define a font name array.And you can add names to this array.

string[] fontName = new string[] { "Comic Sans MS", "Verdana" };
FontFamily f = new FontFamily(fontName[0]);
Tuba
  • 75
  • 8
  • 1
    No, I want this font as fallback font . if character can't show by font "Comic Sans MS", then auto use "Verdana" to show character. – user3435791 Dec 13 '16 at 07:21
1

I think FontFamily is not intended for that task, and I afraid there is no automatic fallback font for Windows Forms. Take note that the sample is for System.Windows.Media from PresentationCore assy and not from System.Drawing.FontFamily. You must try to load a Font manually and then check Name property before trying to assign to Form.Font

You can try this:

Font font = null;
string[] fontNames = {"Comic Sans MS","Verdana"};
foreach (var fontName in fontNames)
{
    font = new Font(fontName, 12.0f);
    if (font.Name == fontName) break;
}
this.Font = font;
Tito
  • 366
  • 4
  • 9
0

You are using WinForms, but looking at the WPF documentation. The WinForms FontFamily class constructor doesn't support explicit fallback fonts. Only one font's face name can be specified for the FontFamily constructor. If it can't find that specified font, the operating system automatically selects a fallback, based on various heuristics (this article is very old, so some of these details have changed, but many of them are the same).

In general, WinForms applications should not hardcode a particular font. What you want to use in your UI is the standard, system dialog font, which you can obtain using SystemFonts.MessageBoxFont. Set it in the constructor for each of your forms, so that all child controls automatically pick it up, as advised here. This not only ensures that the font will be available, but it allows your UI to adapt to the user's preferences.

If you absolutely must hard-code a font, then you will need to enumerate the installed fonts and use a manual fallback algorithm—see if the font face you want exists in the enumerated list, and if not, fall back to an alternative. Note that "Comic Sans MS" and "Verdana" are both "safe" fonts; they'll be there on any Windows system that the .NET Framework can run on, unless the user has explicitly removed them. (And they might have. Comic Sans is an abomination.)

Perhaps an even better alternative (again, if you absolutely must have custom, non-standard fonts) would be to bundle the font(s) you need with your application. You can do this by creating a private font collection, which maintains fonts specifically for your application. A private font collection can include font files provided by the application vendor that are not installed on the system.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574