Procedura: disegnare testo verticale in un Windows FormHow to: Draw Vertical Text on a Windows Form

Esempio di codice riportato di seguito viene illustrato come disegnare testo verticale in un form utilizzando la DrawString metodo Graphics.The following code example shows how to draw vertical text on a form by using the DrawString method of Graphics.

EsempioExample

public:
    void DrawVerticalString()
    {
        System::Drawing::Graphics^ formGraphics = this->CreateGraphics();
        String^ drawString = "Sample Text";
        System::Drawing::Font^ drawFont =
            gcnew System::Drawing::Font("Arial", 16);
        System::Drawing::SolidBrush^ drawBrush = gcnew
            System::Drawing::SolidBrush(System::Drawing::Color::Black);
        float x = 150.0F;
        float y = 50.0F;
        System::Drawing::StringFormat^ drawFormat =
            gcnew System::Drawing::StringFormat();
        drawFormat->FormatFlags = StringFormatFlags::DirectionVertical;
        formGraphics->DrawString(drawString, drawFont, drawBrush, x,
            y, drawFormat);
        delete drawFont;
        delete drawBrush;
        delete formGraphics;
    }

public void DrawVerticalString()
{
    System.Drawing.Graphics formGraphics = this.CreateGraphics();
    string drawString = "Sample Text";
    System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 16);
    System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
    float x = 150.0F;
    float y = 50.0F;
    System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
    formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
    drawFont.Dispose();
    drawBrush.Dispose();
    formGraphics.Dispose();
}

Public Sub DrawVerticalString()
    Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
    Dim drawString As String = "Sample Text"
    Dim drawFont As New System.Drawing.Font("Arial", 16)
    Dim drawBrush As New _
        System.Drawing.SolidBrush(System.Drawing.Color.Black)
    Dim x As Single = 150.0
    Dim y As Single = 50.0
    Dim drawFormat As New System.Drawing.StringFormat
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
    formGraphics.DrawString(drawString, drawFont, drawBrush, _
    x, y, drawFormat)
    drawFont.Dispose()
    drawBrush.Dispose()
    formGraphics.Dispose()
End Sub

Compilazione del codiceCompiling the Code

Non è possibile chiamare questo metodo Load gestore dell'evento.You cannot call this method in the Load event handler. Se il form viene ridimensionato o nascosto da un altro formato, il contenuto disegnato non verrà ridisegnato.The drawn content will not be redrawn if the form is resized or obscured by another form. Per ridisegnare automaticamente il contenuto è necessario eseguire l'override di OnPaint metodo.To make your content automatically repaint, you should override the OnPaint method.

Programmazione efficienteRobust Programming

Le seguenti condizioni possono generare un'eccezione:The following conditions may cause an exception:

  • Non è installato il tipo di carattere Arial.The Arial font is not installed.

Vedere ancheSee Also

DrawString
FormatFlags
StringFormatFlags
OnPaint
Introduzione alla programmazione graficaGetting Started with Graphics Programming
Uso di tipi di carattere e testoUsing Fonts and Text