mardi 11 décembre 2012

Créer des graphiques dans une fenêtre.

Ms Chart est très pratique pour construire des graphiques avancés en PowerShell. Par contre il doit être installé sur tous les systèmes qui utiliseront votre application. C'est pour cette raison que pour réaliser des courbes simples, je préfère utiliser les méthodes graphiques intégrées dans PowerShell. Je vous propose donc de voir ici comment créer un graphique ligne par ligne avant de l'afficher dans une PictureBox.

 
#Ouvre une fenêtre.
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object Windows.Forms.Form
$form.text = "Courbes"            
$form.Size = New-Object System.Drawing.Size(200,220)

#Stylo (Pen).
$stylo1 = new-object Drawing.Pen Red   #Couleur
$stylo1.width = 2                      #Taille

#Creation d'un image de 100 pixels sur 100 pixels
$bitmap1 = new-object System.Drawing.Bitmap(100,100)

#Création d'un objet graphique (Graphics).
$graph1 = [System.Drawing.Graphics]::FromImage($bitmap1)

#Trace deux lignes en croix.
$graph1.DrawLine($stylo1, 0, 0, 100, 100)   #Stylo,départ x,départ y,arrivé x,arrivé y
$graph1.DrawLine($stylo1, 0, 100, 100, 0)

#Libère l'objet graphique.
$graph1.Dispose()

#Création d'une image (PictureBox).
$image1 = New-Object System.Windows.Forms.pictureBox
$image1.Location = New-Object Drawing.Point 40,40
$image1.Size = New-Object System.Drawing.Size(100,100)
$image1.backcolor = "white"
$image1.image = $bitmap1

#Attache les contrôles à la fenêtre.
$form.controls.add($image1)

#Affiche le tout.
$form.ShowDialog()

#Fin.

Nous allons voir maintenant comment construire une courbe. En premier je vais créer un fonds dans lequel je vais créer un quadrillage et des repères. Puis je vais tracer dessus ma courbe. Pour afficher le tout, je vais utiliser dans une PictureBox.

 
#Ouvre une fenêtre.
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = New-Object Windows.Forms.Form
$form.text = "Courbes"            
$form.Size = New-Object System.Drawing.Size(400,220)

#Création d'une image de 100 pixels sur 100 pixels.
$bitmap1 = new-object System.Drawing.Bitmap(300,100)

#Création d'un objet graphique (Graphics).
$graph1 = [System.Drawing.Graphics]::FromImage($bitmap1)

#Trace une grille.
$stylo1 = new-object Drawing.Pen LightGray     #Couleur du stylo.
$stylo1.width = 1                              #Taille du stylo.
$graph1.DrawLine($stylo1, 0, 25, 300, 25)      #Ligne honrizontale.
$graph1.DrawLine($stylo1, 0, 50, 300, 50)      #Stylo,début x,début y,fin x,fin y.
$graph1.DrawLine($stylo1, 0, 75, 300, 75)
$graph1.DrawLine($stylo1, 100, 25, 100, 100)   #Ligne verticale.
$graph1.DrawLine($stylo1, 200, 25, 200, 100)

#Ajoute les dates et heures dans la grille.
#FromArgb(Alpha,Rouge,Vert,Bleu).
#Font(Police, taille).
$police1 = new-object System.Drawing.Font( "Verdana", 10 )
$brush1 = New-Object Drawing.SolidBrush( [System.Drawing.Color]::FromArgb(255,0,0,0) )
$graph1.DrawString("7h00", $police1, $brush1, 2, 80)   #Texte,police,brush,x,y.
$graph1.DrawString("8h00", $police1, $brush1, 102, 80)
$graph1.DrawString("9h00", $police1, $brush1, 202, 80)
$graph1.DrawString("10-12-2012", $police1, $brush1, 2, 4)

#Tracer la courbe.
$stylo2 = new-object Drawing.Pen Red         #Couleur du stylo.
$stylo2.width = 2                            #Taille du stylo.
$graph1.DrawLine($stylo2, 0, 42, 72, 70)     #Stylo,début x,début y,fin x,fin y.
$graph1.DrawLine($stylo2, 72, 70, 150, 30)
$graph1.DrawLine($stylo2, 150, 30, 300, 61)

#Libère l'objet graphique.
$graph1.Dispose()

#Création d'une image (PictureBox).
$image1 = New-Object System.Windows.Forms.pictureBox
$image1.Location = New-Object Drawing.Point 40,40
$image1.Size = New-Object System.Drawing.Size(300,100)
$image1.backcolor = "white"
$image1.image = $bitmap1

#Attache les contrôles à la fenêtre.
$form.controls.add($image1)

#Affiche le tout.
$form.ShowDialog()

#Fin.