Comment : écrire du texte à un emplacement spécifié

Lorsque vous effectuez un dessin personnalisé, vous pouvez dessiner du texte dans une seule ligne horizontale commençant à un point spécifié. Vous pouvez dessiner du texte de cette façon à l’aide de la DrawString méthode surchargée de la Graphics classe qui prend un ou PointF un Point paramètre. La DrawString méthode nécessite également un Brush et Font

Vous pouvez également utiliser la DrawText méthode surchargée de l’élément TextRenderer qui prend un Point. DrawText nécessite également un Color et un Font.

L’illustration suivante montre la sortie du texte dessiné à un point spécifié lorsque vous utilisez la DrawString méthode surchargée.

Screenshot that shows the output of text at a specified point.

Pour dessiner une ligne de texte avec GDI+

  1. Utilisez la DrawString méthode, en passant le texte souhaité, Point ou PointF, Fontet Brush.

    using (Font font1 = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)){
    PointF pointF1 = new PointF(30, 10);
    e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1);
    }
    
    Dim font1 As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)
    Try
        Dim pointF1 As New PointF(30, 10)
        e.Graphics.DrawString("Hello", font1, Brushes.Blue, pointF1)
    Finally
        font1.Dispose()
    End Try
    

Pour dessiner une ligne de texte avec GDI

  1. Utilisez la DrawText méthode, en passant le texte souhaité, Point, Fontet Color.

    using (Font font = new Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel))
    {
        Point point1 = new Point(30, 10);
        TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue);
    }
    
    Dim font As New Font("Times New Roman", 24, FontStyle.Bold, GraphicsUnit.Pixel)
    Try
        Dim point1 As New Point(30, 10)
        TextRenderer.DrawText(e.Graphics, "Hello", font, point1, Color.Blue)
    Finally
        font.Dispose()
    End Try
    

Compilation du code

Les exemples précédents nécessitent :

Voir aussi