0

我有我写的这段代码,但是当我左键单击表单时使用了这段代码,但不是随机数的圆圈。我想知道当我单击一个按钮时,如何调整它以在表单上绘制 5-10 个随机颜色的随机圆圈,但它必须使用循环。谢谢!!

Private Sub CirclePainterForm_MouseDown(ByVal sender As _
    Object, ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles MyBase.MouseDown

        Dim graphicsObject As Graphics = CreateGraphics()
        Dim randomObject As Random = New Random
        Dim diameter As Integer = randomObject.Next(5, 200)

        If e.Button = Windows.Forms.MouseButtons.Left Then
            graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X, e.Y, _
            diameter, diameter)

            graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 250, e.Y, _
            diameter + 50, diameter + 50)

            graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 500, e.Y, _
            diameter + 75, diameter + 75)

            graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 750, e.Y, _
            diameter + 100, diameter + 100)

            graphicsObject.FillEllipse(New SolidBrush(RandomColor()), e.X + 1000, e.Y, _
            diameter + 125, diameter + 125)
4

1 回答 1

0

在表单中添加一个按钮并为 Click 事件添加代码:

做圆形或方形[并更新您/我的评论]

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim rnd As New Random(), rndCol As New Random(DateTime.Now.Ticks Mod ((rnd.Next) + 1))
    Dim numShapes = rnd.Next(5, 11), bIsCircleOrSquare As Boolean = (rnd.Next Mod 2)

    Using g = Me.CreateGraphics()
        g.Clear(Me.BackColor)
        Dim diam = rnd.Next(55, Math.Min(255, ClientSize.Width))
        While numShapes > 0
            Using b As New SolidBrush(Color.FromArgb(rndCol.Next(100, 256), rndCol.Next(256), rndCol.Next(256), rndCol.Next(256)))
                Dim x = rnd.Next(ClientSize.Width - diam), y = rnd.Next(ClientSize.Height - diam)
                If bIsCircleOrSquare Then
                    g.FillRectangle(b, x, y, diam, diam)
                    g.DrawRectangle(Pens.Black, x, y, diam, diam)
                Else
                    g.FillEllipse(b, x, y, diam, diam)
                    g.DrawEllipse(Pens.Black, x, y, diam, diam)
                End If
            End Using
            numShapes -= 1
        End While
    End Using

End Sub

如果您想看到圆圈重叠,您可以替换:

 Using b As New SolidBrush(Color.FromArgb(rndCol.Next(100,256), rndCol.Next(256), rndCol.Next(256), rndCol.Next(256)))

 Using b As New SolidBrush(Color.FromArgb(255, rndCol.Next(256), rndCol.Next(256), rndCol.Next(256)))
于 2013-02-24T02:27:54.477 回答