Natürlich geht das, nur musst du etwas mehr über Grafik verstehen. Du solltest nicht direkt auf eine Form zeichnen, das geht nur solange die Form nie versteckt/minimisiert wird. Dann ist deine Grafik weg. Painting solltest du in Form_Paint() oder OnPaint() erledigen. In dem Fall wird standardmässig zuerst den Hintergrund neu gemacht, was alles löscht. Eine Graphics.Clear() brauchst du gar nicht. Dann zeichnest du die Grafikobjekte, die du sehen willst.
Dafür ist es sehr hilfreich ein paar Hilfsklassen zu haben, die definieren was gezeichnet werden soll. Die kannst du in etwa einem Array zusamennfassen. Dann zeichnest du alles im Array. Hier ist ein komplettes (wenn relativ einfaches) Beispiel. GraphicsStructure ist ein Behälter für weitere GraphicsItem Objekte, und ist selbst ein GraphicsItem. Das ermöglicht, dass ein komplettes Objekt aus mehreren Unterobjekten hinzugefügt (oder gelöscht) und vor allem gezeichnet werden kann.
Hier wird übrigens in einem Panel gezeichnet, aber das kann auch eine Form sein. Mit dem RemoveButton wird übrigens ein Objekt gelöscht, ohne die anderen zu beeinflussen.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents AddPanelButton As System.Windows.Forms.Button
Friend WithEvents RenewButton As System.Windows.Forms.Button
Friend WithEvents RemoveButton As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.AddPanelButton = New System.Windows.Forms.Button
Me.RenewButton = New System.Windows.Forms.Button
Me.RemoveButton = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'AddPanelButton
'
Me.AddPanelButton.Location = New System.Drawing.Point(8, 240)
Me.AddPanelButton.Name = "AddPanelButton"
Me.AddPanelButton.Size = New System.Drawing.Size(96, 24)
Me.AddPanelButton.TabIndex = 3
Me.AddPanelButton.Text = "Add Panel"
'
'RenewButton
'
Me.RenewButton.Location = New System.Drawing.Point(112, 240)
Me.RenewButton.Name = "RenewButton"
Me.RenewButton.Size = New System.Drawing.Size(112, 24)
Me.RenewButton.TabIndex = 4
Me.RenewButton.Text = "Renew"
'
'RemoveButton
'
Me.RemoveButton.Location = New System.Drawing.Point(8, 272)
Me.RemoveButton.Name = "RemoveButton"
Me.RemoveButton.Size = New System.Drawing.Size(96, 24)
Me.RemoveButton.TabIndex = 5
Me.RemoveButton.Text = "Remove"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 301)
Me.Controls.Add(Me.RemoveButton)
Me.Controls.Add(Me.RenewButton)
Me.Controls.Add(Me.AddPanelButton)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private panelCount As Integer
Private pos As New Point(0, 0)
Private pic As GraphicsStructure
Private gi As GraphicsItem
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
' Create simple GraphicsStructure
pic = New GraphicsStructure
pic.AddItem(New Line(New Point(10, 10), New Point(50, 50), New Pen( _
Color.Red, 3)))
pic.AddItem(New Line(New Point(20, 15), New Point(40, 63), New Pen( _
Color.Green, 3)))
gi = New FilledBox(New Rectangle(5, 5, 10, 10), New SolidBrush( _
Color.Blue))
pic.AddItem(gi)
End Sub
Private Sub AddPanelButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles AddPanelButton.Click
Dim GraphicsPanel As New GraphicsPanel
GraphicsPanel.Location = pos
GraphicsPanel.Name = "Panel" & panelCount.ToString()
GraphicsPanel.Size = New Size(100, 100)
GraphicsPanel.TabIndex = panelCount
GraphicsPanel.BackColor = Color.Cornsilk
GraphicsPanel.Picture = pic
Controls.Add(GraphicsPanel)
panelCount += 1
GraphicsPanel.BringToFront()
pos.Offset(50, 50)
End Sub
Private Sub RemoveButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles RemoveButton.Click
pic.RemoveItem(gi)
Me.Refresh()
End Sub
Private Sub RenewButton_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles RenewButton.Click
pic.Dispose()
' Create GraphicsStructure with two substructures
pic = New GraphicsStructure
Dim pic1 As GraphicsStructure
pic1 = New GraphicsStructure(New Point(30, 30))
pic1.AddItem(New Line(New Point(15, 18), New Point(22, 60), New Pen( _
Color.BlueViolet, 5)))
pic1.AddItem(New Line(New Point(10, 32), New Point(45, 55), New Pen( _
Color.HotPink, 5)))
gi = New FilledBox(New Rectangle(30, 30, 15, 15), New SolidBrush( _
Color.Coral))
pic1.AddItem(gi)
pic.AddItem(pic1)
pic1 = New GraphicsStructure(New Point(10, 10))
pic.AddItem(New Line(New Point(15, 18), New Point(22, 60), New Pen( _
Color.BurlyWood, 2)))
pic.AddItem(New Line(New Point(10, 32), New Point(45, 55), New Pen( _
Color.Lavender, 2)))
pic.AddItem(pic1)
Dim al As New ArrayList
For Each c As Control In Me.Controls
If TypeOf c Is GraphicsPanel Then
al.Add(c)
End If
Next
For Each c As Control In al
Me.Controls.Remove(c)
Next
panelCount = 0
pos = New Point(0, 0)
Me.Refresh()
End Sub
Private Class GraphicsPanel
Inherits Panel
Private pic As GraphicsStructure
Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.DoubleBuffer, True)
Me.UpdateStyles()
End Sub
Public Property Picture() As GraphicsStructure
Get
Return pic
End Get
Set(ByVal Value As GraphicsStructure)
pic = Value
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As _
System.Windows.Forms.PaintEventArgs)
pic.Render(e.Graphics)
End Sub
End Class
' Base Class
Public MustInherit Class GraphicsItem
Implements IDisposable
Public MustOverride Sub Render(ByVal g As Graphics)
Public MustOverride Sub Dispose() Implements IDisposable.Dispose
End Class
' GraphicsItem Collection
Public Class GraphicsStructure
Inherits GraphicsItem
Private loc As New Point(0, 0)
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal loc As Point)
MyBase.New()
Me.loc = loc
End Sub
Private items As New ArrayList
Public Sub AddItem(ByVal item As GraphicsItem)
items.Add(item)
End Sub
Public Sub RemoveItem(ByVal item As GraphicsItem)
items.Remove(item)
End Sub
Public Overrides Sub Render(ByVal g As System.Drawing.Graphics)
Dim gc As System.Drawing.Drawing2D.GraphicsContainer = _
g.BeginContainer()
g.TranslateTransform(loc.X, loc.Y)
For Each gi As GraphicsItem In items
gi.Render(g)
Next
g.EndContainer(gc)
End Sub
Public Overrides Sub Dispose()
For Each gi As GraphicsItem In items
gi.Dispose()
Next
End Sub
End Class
' Line GraphicsItem
Public Class Line
Inherits GraphicsItem
Private p1 As Point
Private p2 As Point
Private p As Pen
Public Sub New(ByVal p1 As Point, ByVal p2 As Point, ByVal p As Pen)
Me.p1 = p1
Me.p2 = p2
Me.p = p
End Sub
Public Overrides Sub Render(ByVal g As Graphics)
g.DrawLine(p, p1, p2)
End Sub
Public Overrides Sub Dispose()
p.Dispose()
End Sub
End Class
' FilledBox GraphicsItem
Public Class FilledBox
Inherits GraphicsItem
Private rect As Rectangle
Private b As Brush
Public Sub New(ByVal rect As Rectangle, ByVal b As Brush)
Me.rect = rect
Me.b = b
End Sub
Public Overrides Sub Render(ByVal g As Graphics)
g.FillRectangle(b, rect)
End Sub
Public Overrides Sub Dispose()
b.Dispose()
End Sub
End Class
End Class ________
Alle Angaben ohne Gewähr. Keine Haftung für Vorschläge, Tipps oder sonstige Hilfe, falls es schiefgeht, nur Zeit verschwendet oder man sonst nicht zufrieden ist |