Quellcode Grafikprogramm 1: Einfaches Malprogramm

Public Class Form1

'____________________________________________________________________________________

    Dim X As Integer ' Mauskoordinaten

    Dim Y As Integer ' Mauskoordinaten

    Dim flag As Integer 'Maus

    Dim Pinselform As Integer 'Rund, eckig oder flach

    Dim groesse As Integer 'Pinseldurchmesser

    ' Malfarbe __________________________________________________________________________

    Dim rot As Integer

    Dim grün As Integer

    Dim blau As Integer

    ' Hintergrundfarbe ___________________________________________________________________

    Dim rot2 As Integer

    Dim grün2 As Integer

    Dim blau2 As Integer

    'Screenshots ________________________________________________________________________

    Dim b As New Bitmap(600, 400) ' Größe des zu speichernden Bereiches festlegen

    Dim g As Graphics = Graphics.FromImage(b)

    Dim mypic As Bitmap

    'Datei speichern und laden ____________________________________________________________

    Dim Dateiname As String

 

   ' Programmstart ______________________________________________________________________

   ' Voreinstellungen

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e _

      As System.EventArgs) Handles MyBase.Load

        groesse = 8

        Pinselform = 1

        ' Beim Laden des Programms wird die Sub "paste" einmal ausgeführt. Ansonsten könnte 

        ' das Bild später nicht gespeichert werden.

        paste()

    End Sub

 

 ' Mausfunktionen ______________________________________________________________________

   Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e _

    As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown

        'Beim Drücken der Maustaste wird flag auf 1 gesetzt. Die Mauskoordinaten werden 

        ' abgefragt und es wird genau ein "Pinselklecks" gezeichnet.

        flag = 1

        X = e.X

        Y = e.Y

        malen()

    End Sub

 

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e _

    As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        ' Jetzt wird die Maus bewegt. Ist gleichzeitig die linke Maustaste gedrückt

        '(flag also gleich 1), so werden wieder die Mauskoordinaten abgefragt und zum malen 

        'zur Sub "malen" verzweigt.

        If flag = 1 Then

            X = e.X

            Y = e.Y

            malen()

        End If

    End Sub

 

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e _

    As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp

        'Wird die Maus losgelassen, so wird flag auf 0 gesetzt. Auch wenn die Maus nun

        'weiter bewegt wird, kann nicht mehr gezeichnet werden.

        flag = 0

    End Sub

 

   ' Pinselgrößen _______________________________________________________________________

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  _
   Handles Button9.Click

    groesse = 32

    End Sub

   

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e _

    As System.EventArgs) Handles Button4.Click

        'Die folgenden Subs bestimmen Pinselgröße und -Form

        groesse = 8

    End Sub

   

   Private Sub Button5_Click(ByVal sender As System.Object, ByVal e _

    As System.EventArgs) Handles Button5.Click

        groesse = 4

    End Sub

   

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As _

    System.EventArgs) Handles Button6.Click

        groesse = 2

    End Sub

   ' Pinselform _________________________________________________________________________

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles Button1.Click

        Pinselform = 1

    End Sub

   

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As _

    System.EventArgs) Handles Button2.Click

        Pinselform = 2

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As _

    System.EventArgs) Handles Button3.Click

        Pinselform = 3

    End Sub

   

    '  Löschen des Bildschirms zum Neuzeichnen ______________________________________________

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e _

    As System.EventArgs) Handles Button7.Click

       

        PictureBox1.CreateGraphics.Clear(Color.FromArgb(rot2, grün2, blau2))

    End Sub

   

   ' Funktionen zum Zeichnen _____________________________________________________________

    Private Sub malen()

        'Hier wird gemalt. Je nach gewählter Pinselform werden gefüllte Kreise, 

        'flache Ellipsen oder Rechtecke gezeichnet.

        If Pinselform = 1 Then

            PictureBox1.CreateGraphics.FillEllipse(New SolidBrush _

            (System.Drawing.Color.FromArgb(rot, grün, blau)), X, Y, groesse, groesse)

        End If

        If Pinselform = 2 Then

            PictureBox1.CreateGraphics.FillRectangle(New SolidBrush _

            (System.Drawing.Color.FromArgb(rot, grün, blau)), X, Y, groesse, groesse)

        End If

       

       If Pinselform = 3 Then

            PictureBox1.CreateGraphics.FillEllipse(New SolidBrush _

            (System.Drawing.Color.FromArgb(rot, grün, blau)), X, Y, groesse * 4, groesse)

        End If

    End Sub

   ' __________________________________________________________________________________

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e _

    As System.EventArgs) Handles PictureBox1.Click

        'Diese leere Sub wird vom Programm automatisch angelegt, auch wenn sie 

        'keinen Code enthält.

     End Sub

   ' __________________________________________________________________________________

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e _

    As System.EventArgs) Handles Button14.Click

        'ColorDialog-Button

        ColorDialog1.ShowDialog()

        rot = ColorDialog1.Color.R

        grün = ColorDialog1.Color.G

        blau = ColorDialog1.Color.B

    End Sub

 

    ' Speichern der Zeichnung _____________________________________________________________

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e _

    As System.EventArgs) Handles Button10.Click

     

        'zunächst müssen die Funktionen Copy und Paste im Hintergrund ausgeführt werden

        copy()

        paste()

        SaveFileDialog1.InitialDirectory = FolderBrowserDialog1.SelectedPath

        SaveFileDialog1.ShowDialog()

    End Sub

 

   '  Laden der Zeichnung ________________________________________________________________

   Private Sub Button11_Click(ByVal sender As System.Object, ByVal e _

    As System.EventArgs) Handles Button11.Click

        OpenFileDialog1.InitialDirectory = FolderBrowserDialog1.SelectedPath

        OpenFileDialog1.ShowDialog()

    End Sub

 

   ' Hintergrundfarbe ____________________________________________________________________

   Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

        ColorDialog1.ShowDialog()

        rot2 = ColorDialog1.Color.R

        grün2 = ColorDialog1.Color.G

        blau2 = ColorDialog1.Color.B

         PictureBox1.CreateGraphics.FillRectangle(New SolidBrush _

            (System.Drawing.Color.FromArgb(rot2, grün2, blau2)), _

           New Rectangle(0, 0, 600, 400))

    End Sub

   

  Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e _

  As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

        'Laden der Zeichnung

        mypic = New Bitmap(OpenFileDialog1.FileName)

        PictureBox1.Image = mypic

    End Sub

' Hintergrundfunktionen__________________________________________________________________

    Private Sub SaveFileDialog1_FileOk(ByVal sender As System.Object, ByVal e _

    As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk

        'Speichern der Zeichnung

        Dateiname = New String(SaveFileDialog1.FileName)

        Dim Bmp As New Bitmap(PictureBox1.Image)

        Bmp.Save(Dateiname, System.Drawing.Imaging.ImageFormat.Bmp)

        Bmp.Dispose()

     End Sub

   '___________________________________________________________________________________

    Private Sub copy()

        'Screenshot des Bildschirms 

        g.CopyFromScreen(Me.Location.X + PictureBox1.Location.X + 10, _

        Me.Location.Y + PictureBox1.Location.Y + 32, 0, 0, b.Size)

    End Sub

    '__________________________________________________________________________________

   Private Sub paste()

        'Screenshot des Bildschirms 

        PictureBox1.Image = b

    End Sub

    '__________________________________________________________________________________

End Class

 

Zurück