Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Mini Cad - Form1.vb

Form1.vb

Caricato da: Poggi Marco
Scarica il programma completo

  1. Imports System
  2. Imports System.Drawing
  3. Imports System.Text
  4.  
  5.  
  6. Public Class Form1
  7.  
  8.     Private tavola As Disegno = Nothing
  9.     Private attuale As Figura = Nothing
  10.     Private pennaSelezionata As Pen
  11.     Private registro As List(Of Figura) = Nothing
  12.  
  13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  14.         Me.Text = "Mini CAD"
  15.         ToolStripStatusLabelCoordinate.Text = ""
  16.         ToolStripStatusLabelComandoInUso.Text = ""
  17.         pennaSelezionata = New Pen(Color.Red, 1)
  18.         tavola = New Disegno(PictureBoxTavola.CreateGraphics())
  19.         registro = New List(Of Figura)(5)
  20.     End Sub
  21.  
  22.     Private Sub PictureBoxTavola_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxTavola.MouseMove
  23.         Dim coordinataY As Integer
  24.         coordinataY = PictureBoxTavola.Height - e.Y
  25.         ToolStripStatusLabelCoordinate.Text = String.Format("x: {0} y: {1}", e.X, coordinataY)
  26.         If Not Figura.ReferenceEquals(attuale, Nothing) Then
  27.             Call rigenera(False)
  28.             If attuale.attivo Then
  29.                 Dim coordinate As Tratto
  30.                 coordinate = attuale.getCoordinate()
  31.                 coordinate.fine.X = e.X
  32.                 coordinate.fine.Y = coordinataY
  33.                 attuale.setCoordinate(coordinate)
  34.                 attuale.disegna(True)
  35.                 ToolStripStatusLabelComandoInUso.Text = attuale.ToString()
  36.             End If
  37.         End If
  38.     End Sub
  39.  
  40.     Private Sub Form1_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
  41.         tavola.cancella()
  42.     End Sub
  43.  
  44.     Private Sub PictureBoxTavola_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBoxTavola.Paint
  45.         Call rigenera(True)
  46.     End Sub
  47.  
  48.     Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
  49.         Call rigenera(False)
  50.     End Sub
  51.  
  52.     Private Sub Form1_Move(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Move
  53.         Call rigenera(True)
  54.     End Sub
  55.  
  56.     Private Sub rigenera(ByVal sfondo As Boolean)
  57.         If Not Disegno.ReferenceEquals(tavola, Nothing) Then
  58.             If sfondo Then
  59.                 tavola.cancella()
  60.             End If
  61.             Dim i As Figura
  62.             For Each i In registro
  63.                 i.disegna(False)
  64.             Next
  65.         End If
  66.     End Sub
  67.  
  68.     Private Sub EsciToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EsciToolStripMenuItem.Click
  69.         Me.Close()
  70.     End Sub
  71.  
  72.     Private Sub LineaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LineaToolStripMenuItem.Click
  73.         If Not Figura.ReferenceEquals(attuale, Nothing) Then
  74.             attuale.cancella()
  75.         End If
  76.         attuale = New Linee(tavola)
  77.         attuale.pennino = New Pen(Color.Red, 2)
  78.         ToolStripStatusLabelComandoInUso.Text = attuale.ToString()
  79.     End Sub
  80.  
  81.     Private Sub AnnullaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnnullaToolStripMenuItem.Click
  82.         If Not Figura.ReferenceEquals(attuale, Nothing) Then
  83.             attuale.cancella()
  84.             attuale = Nothing
  85.             ToolStripStatusLabelComandoInUso.Text = "* Annullato *"
  86.         End If
  87.     End Sub
  88.  
  89.     Private Sub PictureBoxTavola_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxTavola.MouseDown
  90.         If Not Figura.ReferenceEquals(attuale, Nothing) Then
  91.             Dim c As Tratto
  92.             If attuale.attivo Then
  93.                 c = attuale.getCoordinate()
  94.                 c.fine.X = e.X
  95.                 c.fine.Y = PictureBoxTavola.Height - e.Y
  96.                 attuale.setCoordinate(c)
  97.                 attuale.disegna(True)
  98.                 registro.Add(attuale)
  99.                 attuale = Nothing
  100.                 ToolStripStatusLabelComandoInUso.Text = ""
  101.             Else
  102.                 attuale.attivo = True
  103.                 c = attuale.getCoordinate()
  104.                 c.inizio.X = e.X
  105.                 c.inizio.Y = PictureBoxTavola.Height - e.Y
  106.                 attuale.setCoordinate(c)
  107.                 attuale.pennino = pennaSelezionata
  108.             End If
  109.         End If
  110.     End Sub
  111.  
  112.     Private Sub PennaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PennaToolStripMenuItem.Click
  113.         Dim spessore As Integer
  114.         If Integer.TryParse(InputBox("Spessore della linea ?", "Mini CAD", "2"), spessore) Then
  115.             pennaSelezionata.Width = spessore
  116.         End If
  117.         If ColorDialogPenna.ShowDialog() = Windows.Forms.DialogResult.OK Then
  118.             pennaSelezionata.Color = ColorDialogPenna.Color
  119.         End If
  120.     End Sub
  121.  
  122.     Private Sub CerchiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CerchiToolStripMenuItem.Click
  123.         If Not Figura.ReferenceEquals(attuale, Nothing) Then
  124.             attuale.cancella()
  125.             ToolStripStatusLabelComandoInUso.Text = ""
  126.         End If
  127.         attuale = New Cerchi(tavola)
  128.     End Sub
  129.  
  130.     Private Sub EllissiToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EllissiToolStripMenuItem.Click
  131.         If Not Figura.ReferenceEquals(attuale, Nothing) Then
  132.             attuale.cancella()
  133.             ToolStripStatusLabelComandoInUso.Text = ""
  134.         End If
  135.         attuale = New Ellisse(tavola)
  136.     End Sub
  137.  
  138.     Private Sub PoligoniToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PoligoniToolStripMenuItem.Click
  139.         Dim multilato As Poligono
  140.         Dim lati As Integer
  141.         If Not Integer.TryParse(InputBox("Quanti lati ?", "CAD", "3"), lati) Then
  142.             lati = 3
  143.         End If
  144.         multilato = New Poligono(tavola)
  145.         multilato.numeroLati = lati
  146.         If Not Figura.ReferenceEquals(attuale, Nothing) Then
  147.             attuale.cancella()
  148.             ToolStripStatusLabelComandoInUso.Text = ""
  149.         End If
  150.         attuale = multilato
  151.     End Sub
  152.  
  153.     Private Sub DescrizioneToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DescrizioneToolStripMenuItem.Click
  154.         MessageBox.Show("Un semplice CAD", "CAD")
  155.     End Sub
  156.  
  157.     Private Sub ResocontoToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ResocontoToolStripMenuItem.Click
  158.         Dim messaggio As StringBuilder
  159.         Dim sommaArea, sommaperimetro As Double
  160.         sommaArea = 0
  161.         sommaperimetro = 0
  162.         messaggio = New StringBuilder("", 100)
  163.         messaggio.AppendFormat("Presenti {0} figure{1}                                   ", registro.Count, vbCrLf)
  164.         For Each i As Figura In registro
  165.             sommaperimetro += i.perimetro()
  166.             sommaArea += i.area()
  167.         Next
  168.         messaggio.AppendFormat("{0}Area totale: {1:F3}{2}Perimetro totale: {3:F3}", vbCrLf, sommaArea, vbCrLf, sommaperimetro)
  169.         MessageBox.Show(messaggio.ToString(), "Riepilogo")
  170.     End Sub
  171. End Class