好的,所以我正在尝试创建一个程序,每次单击(无关紧要)时,会出现一个随机的彩色圆圈,并且会出现您点击的位置。但是,我可以添加形状的唯一方法是通过Paint事件。这是我现在的代码:
Private Sub Form1_Paint(ByVal Sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Using Brush1 As New SolidBrush(Color.Orange)
e.Graphics.FillEllipse(Brush1, MousePosition.X, MousePosition.Y, 100, 100)
End Using
End Sub
我需要知道我可以在鼠标单击事件中使用的一行代码,它将重新运行此子代码。我知道如何改变大小,并使其随机,我只是不知道如何多次运行这个子,更确切地说;每次单击鼠标后运行此子程序一次。如果有人可以提供帮助,我将不胜感激!
答案 0 :(得分:1)
正如Plutonix所解释的那样,通过调用Invalidate
方法来处理刷新。
你需要记住的是,表面上绘制的任何内容都不是持久性的,因此每次都需要重绘整个屏幕。当然,有许多方法可以针对性能目的对其进行优化,因为此过程可能会占用大量CPU资源;特别是,因为GDI +不是硬件加速的。
所以,你需要做的是:
记录每次点击(x,y位置)并存储
由于每个圆的半径是随机的,因此在用户点击表单时确定半径,然后将其与点击的x,y位置一起存储
然后,让Paint
事件重新绘制每个存储的点击序列(及其各自的半径)并反复绘制每个圆圈。
这是一个可以解决问题的实现。只需将此代码粘贴到任何Form的类中即可对其进行测试:
Private Class Circle
Public ReadOnly Property Center As Point
Public ReadOnly Property Radius As Integer
Public Sub New(center As Point, radius As Integer)
Me.Center = center
Me.Radius = radius
End Sub
End Class
Private circles As New List(Of Circle)
Private radiusRandomizer As New Random()
Private Sub FormLoad(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True) ' Not really necessary in this app...
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.UserPaint, True)
End Sub
Private Sub FormMouseClick(sender As Object, e As MouseEventArgs) Handles Me.MouseClick
circles.Add(New Circle(New Point(e.X, e.Y), radiusRandomizer.Next(10, 100)))
Me.Invalidate()
End Sub
Private Sub FormPaint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Dim g As Graphics = e.Graphics
g.Clear(Color.Black)
Using p As New Pen(Color.White)
For Each c In circles
g.DrawEllipse(p, c.Center.X - c.Radius \ 2, c.Center.Y - c.Radius \ 2, c.Radius, c.Radius)
Next
End Using
End Sub