Imports System
Imports System.Drawing
Imports System.Text
Public Class Form1
Private tavola As Disegno = Nothing
Private attuale As Figura = Nothing
Private pennaSelezionata As Pen
Private registro As List(Of Figura) = Nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Mini CAD"
ToolStripStatusLabelCoordinate.Text = ""
ToolStripStatusLabelComandoInUso.Text = ""
pennaSelezionata = New Pen(Color.Red, 1)
tavola = New Disegno(PictureBoxTavola.CreateGraphics())
registro = New List(Of Figura)(5)
End Sub
Private Sub PictureBoxTavola_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxTavola.MouseMove
Dim coordinataY As Integer
coordinataY = PictureBoxTavola.Height - e.Y
ToolStripStatusLabelCoordinate.Text = String.Format("x: {0} y: {1}", e.X, coordinataY)
If Not Figura.ReferenceEquals(attuale, Nothing) Then
Call rigenera(False)
If attuale.attivo Then
Dim coordinate As Tratto
coordinate = attuale.getCoordinate()
coordinate.fine.X = e.X
coordinate.fine.Y = coordinataY
attuale.setCoordinate(coordinate)
attuale.disegna(True)
ToolStripStatusLabelComandoInUso.Text = attuale.ToString()
End If
End If
End Sub
Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
tavola.cancella()
End Sub
Private Sub PictureBoxTavola_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBoxTavola.Paint
Call rigenera(True)
End Sub
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Call rigenera(False)
End Sub
Private Sub Form1_Move(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Move
Call rigenera(True)
End Sub
Private Sub rigenera(ByVal sfondo As Boolean)
If Not Disegno.ReferenceEquals(tavola, Nothing) Then
If sfondo Then
tavola.cancella()
End If
Dim i As Figura
For Each i In registro
i.disegna(False)
Next
End If
End Sub
Private Sub EsciToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EsciToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub LineaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LineaToolStripMenuItem.Click
If Not Figura.ReferenceEquals(attuale, Nothing) Then
attuale.cancella()
End If
attuale = New Linee(tavola)
attuale.pennino = New Pen(Color.Red, 2)
ToolStripStatusLabelComandoInUso.Text = attuale.ToString()
End Sub
Private Sub AnnullaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnnullaToolStripMenuItem.Click
If Not Figura.ReferenceEquals(attuale, Nothing) Then
attuale.cancella()
attuale = Nothing
ToolStripStatusLabelComandoInUso.Text = "* Annullato *"
End If
End Sub
Private Sub PictureBoxTavola_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxTavola.MouseDown
If Not Figura.ReferenceEquals(attuale, Nothing) Then
Dim c As Tratto
If attuale.attivo Then
c = attuale.getCoordinate()
c.fine.X = e.X
c.fine.Y = PictureBoxTavola.Height - e.Y
attuale.setCoordinate(c)
attuale.disegna(True)
registro.Add(attuale)
attuale = Nothing
ToolStripStatusLabelComandoInUso.Text = ""
Else
attuale.attivo = True
c = attuale.getCoordinate()
c.inizio.X = e.X
c.inizio.Y = PictureBoxTavola.Height - e.Y
attuale.setCoordinate(c)
attuale.pennino = pennaSelezionata
End If
End If
End Sub
Private Sub PennaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PennaToolStripMenuItem.Click
Dim spessore As Integer
If Integer.TryParse(InputBox("Spessore della linea ?", "Mini CAD", "2"), spessore) Then
pennaSelezionata.Width = spessore
End If
If ColorDialogPenna.ShowDialog() = Windows.Forms.DialogResult.OK Then
pennaSelezionata.Color = ColorDialogPenna.Color
End If
End Sub
Private Sub CerchiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CerchiToolStripMenuItem.Click
If Not Figura.ReferenceEquals(attuale, Nothing) Then
attuale.cancella()
ToolStripStatusLabelComandoInUso.Text = ""
End If
attuale = New Cerchi(tavola)
End Sub
Private Sub EllissiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EllissiToolStripMenuItem.Click
If Not Figura.ReferenceEquals(attuale, Nothing) Then
attuale.cancella()
ToolStripStatusLabelComandoInUso.Text = ""
End If
attuale = New Ellisse(tavola)
End Sub
Private Sub PoligoniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PoligoniToolStripMenuItem.Click
Dim multilato As Poligono
Dim lati As Integer
If Not Integer.TryParse(InputBox("Quanti lati ?", "CAD", "3"), lati) Then
lati = 3
End If
multilato = New Poligono(tavola)
multilato.numeroLati = lati
If Not Figura.ReferenceEquals(attuale, Nothing) Then
attuale.cancella()
ToolStripStatusLabelComandoInUso.Text = ""
End If
attuale = multilato
End Sub
Private Sub DescrizioneToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DescrizioneToolStripMenuItem.Click
MessageBox.Show("Un semplice CAD", "CAD")
End Sub
Private Sub ResocontoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResocontoToolStripMenuItem.Click
Dim messaggio As StringBuilder
Dim sommaArea, sommaperimetro As Double
sommaArea = 0
sommaperimetro = 0
messaggio = New StringBuilder("", 100)
messaggio.AppendFormat("Presenti {0} figure{1} ", registro.Count, vbCrLf)
For Each i As Figura In registro
sommaperimetro += i.perimetro()
sommaArea += i.area()
Next
messaggio.AppendFormat("{0}Area totale: {1:F3}{2}Perimetro totale: {3:F3}", vbCrLf, sommaArea, vbCrLf, sommaperimetro)
MessageBox.Show(messaggio.ToString(), "Riepilogo")
End Sub
End Class