我有一个带有平面图图片的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
我想做的是抓住那个物体,以便我以后可以改变背景颜色等。
我该怎么办?
答案 0 :(得分:0)
他们通常会采用以下两种方式:
如果您选择执行后者,我建议您创建自己的UserControl
,其中包含有关每个图形对象的所有必要数据,例如多边形中的点。要动态加载它们,您可以执行以下操作:
Private Sub AddGraphicalObject(properties As GraphicalObjectProperties)
Dim control As New GraphicalObjectControl()
control.Properties = properties
' set position and size
Panel1.Controls.Add(control)
End Sub
您可能希望在添加控件时为控件上的某些事件添加一些事件处理程序。