次の方法で共有


方法 : 描画テキストを配置する

更新 : 2007 年 11 月

カスタムな描画を実行するとき、フォームまたはコントロールに描画テキストを中央揃えで配置することがよくあります。描画テキストの配置は、DrawString メソッドまたは DrawText メソッドを使用し、正しい書式設定オブジェクトの作成と適切な書式設定フラグの設定を行うことによって簡単に実行できます。

GDI+ (DrawString) を使用して、テキストを中央揃えで描画するには

  • StringFormat を適切な DrawString メソッドに使用して、テキストの中央揃えを指定します。

    Dim text1 As String = "Use StringFormat and Rectangle objects to" & _
        " center text in a rectangle."
    Dim font1 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect1 As New Rectangle(10, 10, 130, 140)
    
        ' Create a StringFormat object with the each line of text, and the block
        ' of text centered on the page.
        Dim stringFormat As New StringFormat()
        stringFormat.Alignment = StringAlignment.Center
        stringFormat.LineAlignment = StringAlignment.Center
    
        ' Draw the text and the surrounding rectangle.
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat)
        e.Graphics.DrawRectangle(Pens.Black, rect1)
    Finally
        font1.Dispose()
    End Try
    
    string text1 = "Use StringFormat and Rectangle objects to" 
        + " center text in a rectangle.";
    using (Font font1 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect1 = new Rectangle(10, 10, 130, 140);
    
        // Create a StringFormat object with the each line of text, and the block
        // of text centered on the page.
        StringFormat stringFormat = new StringFormat();
        stringFormat.Alignment = StringAlignment.Center;
        stringFormat.LineAlignment = StringAlignment.Center;
    
        // Draw the text and the surrounding rectangle.
        e.Graphics.DrawString(text1, font1, Brushes.Blue, rect1, stringFormat);
        e.Graphics.DrawRectangle(Pens.Black, rect1);
    }
    

GDI (DrawText) を使用して、テキストを中央揃えで描画するには

  • テキストの折り返し、および垂直と水平方向の中央揃えのために、TextFormatFlags 列挙値を適切な DrawText メソッドに使用します。

    Dim text2 As String = "Use TextFormatFlags and Rectangle objects to" & _
            " center text in a rectangle."
    
    Dim font2 As New Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point)
    Try
        Dim rect2 As New Rectangle(150, 10, 130, 140)
    
        ' Create a TextFormatFlags with word wrapping, horizontal center and
        ' vertical center specified.
        Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or _
            TextFormatFlags.VerticalCenter Or TextFormatFlags.WordBreak
    
        ' Draw the text and the surrounding rectangle.
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags)
        e.Graphics.DrawRectangle(Pens.Black, rect2)
    Finally
        font2.Dispose()
    End Try
    
    string text2 = "Use TextFormatFlags and Rectangle objects to"
     + " center text in a rectangle.";
    
    using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
    {
        Rectangle rect2 = new Rectangle(150, 10, 130, 140);
    
        // Create a TextFormatFlags with word wrapping, horizontal center and
        // vertical center specified.
        TextFormatFlags flags = TextFormatFlags.HorizontalCenter |
            TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
    
        // Draw the text and the surrounding rectangle.
        TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
        e.Graphics.DrawRectangle(Pens.Black, rect2);
    }
    

コードのコンパイル方法

上記のコード例は Windows フォームと一緒に使用することが想定されていて、PaintEventArgse が必要です。

参照

処理手順

方法 : GDI を使用してテキストを描画する

方法 : フォント ファミリとフォントを作成する

その他の技術情報

フォントとテキストの使用