方法: 描画テキストを配置するHow to: Align Drawn Text

カスタム描画を実行するときに、フォームやコントロールを描画するテキストを中央揃えにすることがあります多くの場合。When you perform custom drawing, you may often want to center drawn text on a form or control. 描画されるテキストを簡単に合わせることができます、DrawStringまたはDrawTextメソッドを正しく書式設定オブジェクトを作成し、適切な書式指定フラグを設定します。You can easily align text drawn with the DrawString or DrawText methods by creating the correct formatting object and setting the appropriate format flags.

描画するために、GDI + (DrawString) を使用してテキストを中央揃えTo draw centered text with GDI+ (DrawString)

  1. 使用して、StringFormatと適切なDrawString中央揃えのテキストを指定します。Use a StringFormat with the appropriate DrawString method to specify centered text.

    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);
    }
    
    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
    

描画するために、GDI (DrawText) を使用してテキストを中央揃えTo draw centered text with GDI (DrawText)

  1. 使用して、TextFormatFlags列挙体の垂直方向および水平方向には、適切なテキストを中央揃えするほかの折り返しDrawTextメソッド。Use the TextFormatFlags enumeration for wrapping as well as vertically and horizontally centering text with the appropriate DrawText method.

    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);
    }
    
    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
    

コードのコンパイルCompiling the Code

上記のコード例は、Windows フォームで使用するために設計されていて、必要なPaintEventArgseはのパラメーターPaintEventHandlerします。The preceding code examples are designed for use with Windows Forms, and they require PaintEventArgs e, which is a parameter of PaintEventHandler.

関連項目See also