Gewusst wie: Zeichnen von vertikalem Text in einem Windows Form

Im folgenden Codebeispiel wird gezeigt, wie vertikaler Text mithilfe der DrawString-Methode von Graphics auf einem Formular gezeichnet wird.

Beispiel

    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

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

Kompilieren des Codes

Sie können diese Methode nicht im Load-Ereignishandler aufrufen. Der gezeichnete Inhalt wird nicht neu gezeichnet, wenn die Größe des Formulars geändert oder das Formular durch ein anderes Formular verdeckt wird. Wenn der Inhalt automatisch neu gezeichnet werden soll, müssen Sie die OnPaint-Methode überschreiben.

Robuste Programmierung

Die folgenden Bedingungen können einen Ausnahmefehler verursachen:

  • Die Schriftart Arial ist nicht installiert.

Siehe auch

Referenz

DrawString

FormatFlags

StringFormatFlags

OnPaint

Weitere Ressourcen

Erste Schritte mit der Grafikprogrammierung

Verwenden von Schriftarten und Text