使用e.graphics.drawellips在表单上绘制圆圈?

时间:2014-07-15 09:45:02

标签: vb.net-2010

我想在圈内绘制圆圈,我已经尝试过但有困难

这是我的代码

Private Sub DrawCircle()
Dim g As Graphics
g = Panel1.CreateGraphics
Dim yellowPen As New Pen(Color.Yellow, 20)
Dim bluePen As New Pen(Color.Blue, 30)
Dim greenPen As New Pen(Color.Green, 20)
Dim skybluePen As New Pen(Color.AliceBlue, 20)
Dim voiletPen As New Pen(Color.Violet, 15)
Dim blackPen As New Pen(Color.Black, 2)

' Draw ellipses
g.DrawEllipse(yellowPen, 260, 180, 10, 10)

g.DrawEllipse(greenPen, 240, 160, 50, 50)

g.DrawEllipse(bluePen, 220, 140, 90, 90)

g.DrawEllipse(greenPen, 200, 120, 130, 130)

g.DrawEllipse(skybluePen, 180, 100, 170, 170)

g.DrawEllipse(blackPen, 180, 100, 170, 170)

g.DrawEllipse(voiletPen, 170, 90, 190, 190)


End Sub

 Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
DrawCircle()
End Sub

它运行良好但是如果还有其他选项,因为如果你在这段代码中稍微改变一切都会出错,请帮帮我

1 个答案:

答案 0 :(得分:1)

可能是我没有说明你的观点,我认为你正在寻找替代解决方案 这是一个

Private colorForAllCircles As Color
Private ReadOnly rand As New Random

Function RandomColor() As Color
Return Color.FromArgb(rand.Next(0, 256), rand.Next(0, 256), rand.Next(0, 256))
End Function

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim centerX, centerY As Integer
Dim cornerX, cornerY As Integer
Dim radius As Integer
Dim greenPen As New Pen(Brushes.Blue)

centerX = 300
centerY = 200
Dim i As Integer
For i = 20To 200 Step 20
  greenPen = New Pen(RandomColor, 20)
  radius = i
  cornerX = centerX - radius / 2
  cornerY = centerY - radius / 2
  e.Graphics.DrawEllipse(greenPen, cornerX, cornerY, radius, radius)

Next

End Sub