|
-
Nov 29th, 2010, 03:49 PM
#1
Thread Starter
Lively Member
enlarging and reducing the size of a circle using a scrollbar
Hi all,
Im trying to enlarge a circle on a form using a scrollbar. I have managed to do this using the following code:
Code:
Private Sub DrawCircle(ByVal cp As Point, ByVal radius As Integer)
Dim gr As Graphics
gr = Panel1.CreateGraphics
Dim rect As Rectangle = New Rectangle(cp.X - radius, cp.Y - radius, 2 * radius, 2 * radius)
'gr.DrawEllipse(Pens.Black, rect)
gr.DrawEllipse(Pens.Black, rect)
gr.FillEllipse(Brushes.Red, rect)
gr.Dispose()
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
DrawCircle(New Point(TrackBar1.Value / 2, TrackBar1.Value / 2), TrackBar1.Value)
End Sub
Thing is the code draws a circle on every scroll movement. You end up with a mass of circles. I want it to clear previous graphic and just show the circle that relates to the current scrollbar graphic, giving the impression of enlarging and shrinking. Any ideas? All help most appreciated.
-
Nov 29th, 2010, 05:35 PM
#2
Fanatic Member
Re: enlarging and reducing the size of a circle using a scrollbar
Hi.
You need to "clear" the graphics before you draw again, using:
gr.Clear(Color)
Code:
Private Sub DrawCircle(ByVal cp As Point, ByVal radius As Integer)
Dim gr As Graphics
gr = Panel1.CreateGraphics
gr.Clear(Panel1.BackColor)
Dim rect As Rectangle = New Rectangle(cp.X - radius, cp.Y - radius, 2 * radius, 2 * radius)
'gr.DrawEllipse(Pens.Black, rect)
gr.DrawEllipse(Pens.Black, rect)
gr.FillEllipse(Brushes.Red, rect)
gr.Dispose()
End Sub
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
DrawCircle(New Point(TrackBar1.Value / 2, TrackBar1.Value / 2), TrackBar1.Value)
End Sub
-
Nov 29th, 2010, 06:42 PM
#3
Re: enlarging and reducing the size of a circle using a scrollbar
actually it's because he's declaring a new Graphics object each time, hence the multitude of circles. You need to declare the graphics object once, and maintain it.
Dim gr As Graphics is global
Then in your DrawCircle method, you would replace the "Dim gr As Graphics" line with "gr.Clear()"
-
Nov 30th, 2010, 07:50 AM
#4
Fanatic Member
Re: enlarging and reducing the size of a circle using a scrollbar
Yeah, but for the simplicity I just said that. While were at it anyways:
I recommend you to set the following line for the panel to true:
Code:
MyBase.DoubleBuffered = True
It would get rid of the "flashing" while drawing.
The flashing is caused by the "clear" sub, that causes the panel to invalidate and thus redraw.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|