我有一個PictureBox,有一個平面圖。在picturebox上畫多個多邊形
在那個形象,我想繪製多邊形,我可以用下面的代碼:
Private points As New List(Of Point)
Private Sub FlorPlan_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles FlorPlan.MouseDown
points.Add(e.Location)
FlorPlan.Invalidate(New Rectangle(e.X, e.Y, 5, 5))
End Sub
Private Sub FlorPlan_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles FlorPlan.Paint
For Each p As Point In points
e.Graphics.FillRectangle(Brushes.Red, p.X, p.Y, 5, 5)
Next
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Red, 2)
Dim myGraphics As Graphics = PictureBox.CreateGraphics
myGraphics.DrawPolygon(myPen, points.ToArray)
'myGraphics.FillPolygon(Brushes.Red, points.ToArray)
myGraphics.Dispose()
End Sub
我想這樣做是爲了獲得該對象的持有,這樣我以後可以改變背景顏色等。
我該如何繼續?
您需要跟蹤某個自定義類的對象列表中每個圖形對象的所有屬性,否則需要將每個對象顯示爲新控件,並讓每個控件都保留所有這些屬性。每次需要另一個圖形對象時,都可以動態實例化一個新控件。 –
@StevenDoggart 我應該如何動態實例化一個新的控制器? 我試圖創建一個新的圖片框,然後將圖片放入其中,並將新的圖片框添加到現有的圖片框中。 就是這樣嗎? –