Afficher un texte ou des rectangles, ellipses, polygones, etc... inclinés en vb 2008 (2 méthodes possibles)

Description

Comment afficher un texte incliné (angle paramétrable) ou une ellipse inclinée ou même faire une animation en faisant tourner autour d'un point paramétrable.

2 méthodes sous vb 2008 express :

- avec e.Graphics.RotateTransform

- avec Drawing2D.Matrix et GraphicsPath

Source / Exemple :


Imports System.Math

Public Class Form1

#Region "variables globales :"
    Dim x, y As Double 'position de départ de la chaine et de l'ellipse (coin sup gauche)
    Dim angle As Double 'angle de rotation
    Dim pas As Double 'vitesse de l'animation
    Dim larg As Integer ' pour l'ellipse
    Dim haut As Integer 'pour l'ellipse
    Dim crayon As Pen = New Pen(Color.Blue, 3)
#End Region

    'Evenements :
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' initialisation des variables globales :
        x = 400.0F 'coin sup gauche X
        y = 250.0F 'coin sup gauche Y
        larg = 200 'largeur de l'ellipse
        haut = 80 'hauteur de l'ellipse
        angle = 0.0F 'angle de départ (animation)
        pas = 0.1F ' pas de l'angle (animation)
    End Sub
    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        'dessin à faire à chaque rafraichissement de l'écran :
        Dim a, b As Integer 'centre de rotation utilisé pour les 2 méthodes

        a = x - 20 'centre de rotation (X)
        b = y 'centre de rotation (Y)
        DrawRotatedString(e, "Chaine à faire tourner", x, y, a, b, angle) 'afficher la chaine...

        a = x + larg / 2 'centre de l'ellipse (rotation X)
        b = y + haut / 2 'centre de l'ellipse (rotation Y)
        DrawRotatedEllipse(e, x, y, a, b, angle) '-angle -> ferait tourner l'ellipse en sens contraire...

    End Sub
    Private Sub CheckBoxAnim_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxAnim.CheckedChanged

        If CheckBoxAnim.Checked Then Animation()

    End Sub 'bouton animation cliqué
    Private Sub ButtonVitesseMoins_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonVitesseMoins.Click
        pas /= 2
    End Sub 'bouton vitesse -
    Private Sub ButtonVitessePlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonVitessePlus.Click
        pas *= 2
    End Sub 'bouton vitesse +

    'Fonctions :
    Private Sub DrawRotatedEllipse(ByVal e As PaintEventArgs, ByVal x1 As Single, ByVal y1 As Single, _
                                   ByVal a As Single, ByVal b As Single, ByVal ang As Single)
        ' position avant rotation : (x1,y1)
        ' centre voulu pour la rotation  : (a,b)
        ' position intermédiaire après rotation autour de (a,b) : (x2,y2)
        ' position à donner à l'instruction DrawEllipse : (posX,posY)  (déduit de (x2,y2) par rotation -ang)

        Dim x2, y2, posX, posY As Single
        Dim angrad As Single

        ' Set the SmoothingMode to high quality for best readability.
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

        '+ centre de la rotation pour info :
        e.Graphics.DrawEllipse(Pens.Blue, a, b, 2, 2)

        'on transforme l'angle en radians :
        angrad = ang / 180 * PI
        'on calcule la position intermédiaire (rotation ang autour de (a,b) ) :
        x2 = a + (x1 - a) * Cos(angrad) - (y1 - b) * Sin(angrad)
        y2 = b + (x1 - a) * Sin(angrad) + (y1 - b) * Cos(angrad)
        'on calcule la position de départ nécessaire (rotation -ang autour de (0,0)) :
        posX = x2 * Cos(angrad) + y2 * Sin(angrad)
        posY = -x2 * Sin(angrad) + y2 * Cos(angrad)

        ' matrice de rotation appliquée avant le dessin :
        e.Graphics.RotateTransform(ang)
        ' dessiner l'ellipse (le système applique la matrice de rotation avant) :
        e.Graphics.DrawEllipse(crayon, posX, posY, larg, haut)

    End Sub 'méthode avec e.Graphics.RotateTransform

    Public Sub DrawRotatedString(ByVal e As PaintEventArgs, ByVal s As String, ByVal x1 As Single, ByVal y1 As Single, _
                                 ByVal a As Single, ByVal b As Single, ByVal ang As Single)
        ' position avant rotation : (x1,y1)
        ' centre voulu pour la rotation  : (a,b)

        Dim Origine As PointF = New PointF(x1, y1)
        Dim SF As StringFormat = New StringFormat(0)
        Dim Centre As PointF = New PointF(a, b)

        ' Create a GraphicsPath.
        Dim path As New System.Drawing.Drawing2D.GraphicsPath
        ' Declare a matrix that will be used to rotate the text.
        Dim Matrix As New System.Drawing.Drawing2D.Matrix

        ' Set the SmoothingMode to high quality for best readability.
        e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

        ' Add the string to the path; declare the font, font style, size :
        path.AddString(s, Me.Font.FontFamily, 1, 20, Origine, SF)
        ' Set the rotation angle and starting point for the text.
        Matrix.RotateAt(ang, Centre)
        'utiliser  Matrix.Translate() si on veut déplacer le dessin après rotation

        ' Transform the path with the matrix.
        path.Transform(Matrix)
        ' Fill in the path to draw the string :
        e.Graphics.FillPath(Brushes.Red, path) 'tracé lettres pleines
        'e.Graphics.DrawPath(Pens.Red, path) 'tracé contour des lettres seulement

        'path.Reset() 'vide le path si on veut tracer d'autres choses
        'rotateMatrix.Reset() ' et remet à zéro la matrice 

        '+ centre de la rotation pour info :
        e.Graphics.DrawEllipse(Pens.Red, a, b, 2, 2)

        ' Dispose of the path.
        path.Dispose()

    End Sub 'méthode avec Drawing2D.Matrix et GraphicsPath

    Private Sub Animation()
        'lance l'animation

        Do While CheckBoxAnim.Checked
            angle += pas 'variables globales
            If angle > 360 Then angle -= 360.0F
            Me.Refresh()
            System.Windows.Forms.Application.DoEvents()
        Loop

    End Sub

End Class

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.