フォント (Font)

コンストラクタ

フォントファミリー

フォントファミリーの名前を文字列で指定するか、FontFamilyオブジェクトを生成して渡します。

public Font(
    string familyName, // FontFamilyを表す文字列
    float emSize
)
Font(String, Single) - Font コンストラクター (System.Drawing) | Microsoft Learn
new Font("MS ゴシック", 9F);
public Font(
    FontFamily family, // FontFamilyオブジェクト
    float emSize
)
Font(FontFamily, Single) - Font コンストラクター (System.Drawing) | Microsoft Learn
FontFamily ff1 = new FontFamily("MS ゴシック");
FontFamily ff2 = new FontFamily(GenericFontFamilies.SansSerif);

new Font(ff1, 9F);
new Font(ff2, 9F);
new Font(FontFamily.GenericSansSerif, 9F);

いずれの方法でもフォントを生成できますが、インストールされていないフォントファミリーを指定したときの挙動が異なります。

Font f1 = new Font("dummy", 1.0f); // "Microsoft Sans Serif"が代用される

FontFamily ff1 = new FontFamily("dummy");
// 「フォント 'dummy' が見つかりません。」として、System.ArgumentException例外が発生

文字列を指定したときに代替のフォントが使用されたことは、NameとOriginalFontNameプロパティが異なることで検知できます。

FontFamilyクラス

public FontFamily(
    string name
)
FontFamily コンストラクター (String) (System.Drawing) | MSDN

汎用のフォントを指定するならば、GenericFontFamilies列挙型を渡します。

public FontFamily(
    GenericFontFamilies genericFamily
)
FontFamily コンストラクター (GenericFontFamilies) (System.Drawing) | MSDN
GenericFontFamilies列挙型
列挙子 形式
Serif 一般的な、Serifオブジェクト
SansSerif 一般的な、Sans Serifオブジェクト
Monospace 一般的な、Monospaceオブジェクト
GenericFontFamilies 列挙型 (System.Drawing.Text) | MSDN

フォントサイズ

たとえばコントロールのフォントサイズを現在の1.5倍にするには、次のようにします。

this.Font = new Font(
    this.Font.FontFamily,
    this.Font.Size * 1.5F);

フォントスタイル

public Font(
    Font prototype, // 新しいFontを作成するための、既存のFont
    FontStyle newStyle
)
Font(Font, FontStyle) - Font コンストラクター (System.Drawing) | Microsoft Learn

プロパティ

Fontクラスのプロパティはすべて読み取り専用のため、プロパティの値を変更する必要があるときにはオブジェクトを作成し直します。たとえばコントロールのフォントスタイルだけを変更するには、次のようにします。

this.Font = new Font(this.Font, FontStyle.Bold);
プロパティ 内容
string Name フォントの書体名 (typeface name)
string OriginalFontName 最初に指定したフォントの書体名
int Height フォントの行間。つまり、連続するテキスト行のベース ライン間の垂直距離 Height - Font.cs
float Size フォントのemサイズ。Unitプロパティの単位
float SizeInPoints フォントのemサイズ。ポイント (GraphicsUnit.Point) 単位 SizeInPoints - Font.cs
GraphicsUnit Unit フォントの長さの単位
bool Bold  
bool Italic  
     
プロパティ - Font クラス (System.Drawing) | Microsoft Learn

OriginalFontName

指定のフォントがシステムにインストールされていないと代替のフォントが使用されるため、Nameプロパティと異なる文字列となることがあります。

Font font = new Font("dummy", 1.0f);
string str1 = font.Name;             // "Microsoft Sans Serif"
string str2 = font.OriginalFontName; // "dummy"

Unit

GraphicsUnit列挙型
列挙子 内容
World 0 ワールド座標系
Display 1 表示デバイスの長さの単位。通常、ビデオ ディスプレイにはピクセル、プリンターには1/100インチ
Pixel 2 デバイス ピクセル
Point 3 プリンター ポイント (1/72 インチ)
Inch 4 インチ
Document 5 ドキュメント単位 (1/300 インチ)
Millimeter 6 ミリメートル
GraphicsUnit 列挙型 (System.Drawing) | Microsoft Learn

メソッド

Equals()

指定のオブジェクトが、このフォントと同等 (equivalent) であるか評価できます。

public override bool Equals (object obj);
Font.Equals(Object) メソッド (System.Drawing) | Microsoft Learn

objがFontであり、次のプロパティの値が等しければtrueが返されます。

  • FontFamily
  • GdiVerticalFont
  • GdiCharSet
  • Style
  • Size
  • Unit

==演算子では同一であるかで評価されるため、結果が異なります。

Font font1 = new Font(FontFamily.GenericSansSerif, 9F);
Font font2 = new Font(FontFamily.GenericSansSerif, 9F);
Font font3 = font1;

bool r1 = font1 == font2;      // false
bool r2 = font1 == font3;      // true
bool r3 = font1.Equals(font2); // true
bool r4 = font1.Equals(font3); // true

Clone()

Fontのコピーを作成できます。Clone - Font.cs

システムのフォント情報

SystemFontsクラス

コントロール パネルで指定されているフォントの情報を得られます。

プロパティ 用途 Windows 10での既定値
CaptionFont ウィンドウのタイトル バー Yu Gothic UI, 9
DefaultFont ダイアログ ボックスおよびフォームのアプリケーション (既定) MS UI Gothic, 9
DialogFont ダイアログ ボックスおよびフォームのアプリケーション MS UI Gothic, 9
IconTitleFont アイコン タイトル Yu Gothic UI, 11.25
MenuFont メニュー Yu Gothic UI, 9
MessageBoxFont メッセージ ボックス Yu Gothic UI, 9
SmallCaptionFont ツール ウィンドウなどの小さなウィンドウのタイトル バー Yu Gothic UI, 9
StatusFont ステータス バー Yu Gothic UI, 9
プロパティ - SystemFonts クラス (System.Drawing) | Microsoft Learn

InstalledFontCollectionクラス

システムにインストールされているフォントの情報を得られます。InstalledFontCollection クラス (System.Drawing.Text) | Microsoft Learn

文字列との相互変換

Fontに特化したTypeConverterであるFontConverterクラスを用いることで、Fontとstringを相互に変換できます。c# - convert font to string and back again - Stack Overflow

Font font = SystemFonts.DefaultFont;
font.ToString(); // "[Font: Name=MS UI Gothic, Size=9, Units=3, GdiCharSet=128, GdiVerticalFont=False]"

FontConverter converter = new FontConverter();
string str = converter.ConvertToInvariantString(font); // "MS UI Gothic, 9pt"

Font font2 = (Font)converter.ConvertFromInvariantString(str);
font2.ToString(); // "[Font: Name=MS UI Gothic, Size=9, Units=3, GdiCharSet=1, GdiVerticalFont=False]"


Font font3 = (Font)converter.ConvertFromInvariantString("dummy");
font3.ToString(); // "[Font: Name=Microsoft Sans Serif, Size=8.25, Units=3, GdiCharSet=1, GdiVerticalFont=False]"
FontConverter クラス (System.Drawing) | Microsoft Learn
Microsoft Learnから検索