次の方法で共有


Font.GetHeight メソッド (Graphics)

フォントの行間を、指定した Graphics オブジェクトの現在の単位で返します。行間とは、2 つの連続するテキスト行のベース ライン間の垂直距離です。したがって、行間には、文字自体の高さ以外に、行と行の間の空白も含まれます。

Overloads Public Function GetHeight( _
   ByVal graphics As Graphics _) As Single
[C#]
public float GetHeight(Graphicsgraphics);
[C++]
public: float GetHeight(Graphics* graphics);
[JScript]
public function GetHeight(
   graphics : Graphics) : float;

パラメータ

  • graphics
    ページの単位とページのスケールの設定、および表示デバイスの垂直解像度 (dpi) を保持する Graphics オブジェクト。

戻り値

フォントの行間 (ピクセル単位)。

解説

フォントの Unit プロパティが GraphicsUnit.Pixel 以外に設定されている場合、高さ (ピクセル単位) は、指定した Graphics オブジェクトの垂直解像度を使用して計算されます。たとえば、フォント単位がインチ、フォント サイズが 0.3 であるとします。また、対応するフォント ファミリの em の高さが 2048、行間が 2355、 Graphics オブジェクトの Unit が GraphicsUnit.Pixel で、 DpiY が 96 dpi の場合を想定すると、高さは次のように計算できます。

2355*(0.3/2048)*96 = 33.1171875

これと同じ例で、 Graphics オブジェクトの Unit プロパティが GraphicsUnit.Pixel 以外 (たとえば GraphicsUnit.Millimeter など) に設定されている場合を想定すると、mm (ミリメートル) 単位での高さは次のように計算できます (1 インチ = 25.4 mm)。

2355*(0.3/2048)25.4 = 8.762256

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは次のアクションを実行します。

  • Font オブジェクトを作成します。
  • 新しい Font オブジェクトを使用して、画面にテキストの行を描画します。
  • フォントの高さを取得します。
  • 最初の行のすぐ下に、2 つ目のテキスト行を描画します。
 
Public Sub GetHeight_Example(e As PaintEventArgs)
' Create a Font object.
Dim myFont As New Font("Arial", 16)
'Draw text to the screen with myFont.
e.Graphics.DrawString("This is the first line", myFont, _
Brushes.Black, New PointF(0, 0))
'Get the height of myFont.
Dim height As Single = myFont.GetHeight(e.Graphics)
'Draw text immediately below the first line of text.
e.Graphics.DrawString("This is the second line", myFont, _
Brushes.Black, New PointF(0, height))
End Sub
        
[C#] 
public void GetHeight_Example(PaintEventArgs e)
{
// Create a Font object.
Font myFont = new Font("Arial", 16);
//Draw text to the screen with myFont.
e.Graphics.DrawString(
"This is the first line",
myFont,
Brushes.Black,
new PointF(0, 0));
//Get the height of myFont.
float height = myFont.GetHeight(e.Graphics);
//Draw text immediately below the first line of text.
e.Graphics.DrawString(
"This is the second line",
myFont,
Brushes.Black,
new PointF(0, height));
}
        

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Font クラス | Font メンバ | System.Drawing 名前空間 | Font.GetHeight オーバーロードの一覧