英語で読む

次の方法で共有


Font.FromHfont(IntPtr) メソッド

定義

指定した Windows ハンドルから Font を作成します。

C#
public static System.Drawing.Font FromHfont (IntPtr hfont);

パラメーター

hfont
IntPtr

GDI フォントへの Windows ハンドル。

戻り値

このメソッドによって作成される Font

例外

hfont は、TrueType フォントではないオブジェクトを指します。

次のコード例は Windows フォームで使用できるように設計されており、Paint イベント ハンドラーのパラメーターである PaintEventArgseが必要です。 このコードは、次のアクションを実行します。

  • GDI フォントへのハンドルを取得します。

  • そのハンドルから Font を作成します。

  • 新しい Fontを使用して、テキストを画面に描画します。

C#
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr GetStockObject(int fnObject);
public void FromHfont_Example(PaintEventArgs e)
{
             
    // Get a handle for a GDI font.
    IntPtr hFont = GetStockObject(0);
             
    // Create a Font object from hFont.
    Font hfontFont = Font.FromHfont(hFont);
             
    // Use hfontFont to draw text to the screen.
    e.Graphics.DrawString(
        "This font is from a GDI HFONT", hfontFont,Brushes.Black, 
        0, 0);
}

適用対象

製品 バージョン
.NET 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9