vb.net 在图片框上重绘矩形并使方法无效
vb.net redrawing rectangles on picturebox and invalidate method
我正在尝试在具有随机大小和位置的图片框上创建一个矩形。每次用户单击一个按钮时,矩形将被移除并显示新的。
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
RandomBox()
End Sub
Public Sub RandomBox()
Dim r As New Rectangle(CInt(Rnd() * picGene.Width), CInt(Rnd() * picGene.Height), 20 + Rnd() * 50, 10 + Rnd() * 50)
Dim g As System.Drawing.Graphics
g = picGene.CreateGraphics()
g.FillRectangle(Brushes.Blue, r)
End Sub
它始终保留之前绘制的矩形,因此我尝试将 picGene.Invalide() 放在代码块之前或之后。它删除旧矩形但不绘制新矩形。
Public Sub RandomBox()
picGene.Invalidate()
Dim r As New Rectangle(CInt(Rnd() * picGene.Width), CInt(Rnd() * picGene.Height), 20 + Rnd() * 50, 10 + Rnd() * 50)
Dim g As System.Drawing.Graphics
g = picGene.CreateGraphics()
g.FillRectangle(Brushes.Blue, r)
'picGene.Invalidate()
End Sub
有什么想法吗?
谢谢
在内存中存储一个 Rectangle 并在每次单击时更改该矩形并在 paint 事件中对其进行绘制。 Random
class 现在是随机生成值的更好选择。
Private rect As New Rectangle
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
Static rnd As New Random
rect = New Rectangle(rnd.Next(picGene.Width), rnd.Next(picGene.Height), 20 + rnd.Next({somevalue}) * 50, 10 + rnd.Next({someValue}) * 50)
pb.Refresh() 'redraw the rectangle
End Sub
Private Sub picGene_Paint(sender As Object, e As PaintEventArgs) Handles picGene.Paint
e.Graphics.DrawRectangle(Pens.Black, rect)
End Sub
我正在尝试在具有随机大小和位置的图片框上创建一个矩形。每次用户单击一个按钮时,矩形将被移除并显示新的。
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
RandomBox()
End Sub
Public Sub RandomBox()
Dim r As New Rectangle(CInt(Rnd() * picGene.Width), CInt(Rnd() * picGene.Height), 20 + Rnd() * 50, 10 + Rnd() * 50)
Dim g As System.Drawing.Graphics
g = picGene.CreateGraphics()
g.FillRectangle(Brushes.Blue, r)
End Sub
它始终保留之前绘制的矩形,因此我尝试将 picGene.Invalide() 放在代码块之前或之后。它删除旧矩形但不绘制新矩形。
Public Sub RandomBox()
picGene.Invalidate()
Dim r As New Rectangle(CInt(Rnd() * picGene.Width), CInt(Rnd() * picGene.Height), 20 + Rnd() * 50, 10 + Rnd() * 50)
Dim g As System.Drawing.Graphics
g = picGene.CreateGraphics()
g.FillRectangle(Brushes.Blue, r)
'picGene.Invalidate()
End Sub
有什么想法吗?
谢谢
在内存中存储一个 Rectangle 并在每次单击时更改该矩形并在 paint 事件中对其进行绘制。 Random
class 现在是随机生成值的更好选择。
Private rect As New Rectangle
Private Sub btnDraw_Click(sender As Object, e As EventArgs) Handles btnDraw.Click
Static rnd As New Random
rect = New Rectangle(rnd.Next(picGene.Width), rnd.Next(picGene.Height), 20 + rnd.Next({somevalue}) * 50, 10 + rnd.Next({someValue}) * 50)
pb.Refresh() 'redraw the rectangle
End Sub
Private Sub picGene_Paint(sender As Object, e As PaintEventArgs) Handles picGene.Paint
e.Graphics.DrawRectangle(Pens.Black, rect)
End Sub