How do I code pictureboxes move in circlar form round a form

Nonasoft

Member
Joined
May 3, 2007
Messages
16
Programming Experience
1-3
Hello room, I need to design a program that will have some images displayed in pictureboxes. I need to make those picture boxes rotate like in encarta 2004 or any other form possible.

Plz I need somebody that can me give crew or code on hw I can do.
 
Hi. Think of it logically. You must rotate the Image in the picture box x degrees. Then, you will need to rotate the image x + some more degrees.

Strap this on to a Timer.

VB.NET:
Me.PictureBox.Image.RotateFlip(RotateFlipType)
If you just want to Rotate your image in a circle, you will most likely use Rotate90FlipNone.

Sure, that will RotateFlip your image but it won't update.

Thus, you will need to call Me.Refresh() after RotateFlipping your image.
 
Am not saying rotating the in mage inside the picturebox, am taking about rotaing the picture box location itself ranging from one location to another. The way the clock hand turns that is how I want the picturebox rotate on various location circularly on the form like encarta 2004.
 
See the attached sample, I haven't seen Encarta but added 6 Pictureboxes to a form in a circular fashion and used GraphicsPath to get the point references of this circle, then used BackgroundWorker to "animate" them by setting new locations not too fast. I just wrote the name of the Picturebox so it is easy to see which is which when they move around, didn't load any images. This should give you an idea of what you can work with.
 

Attachments

  • vbnet20-PictureboxCircle.zip
    17.8 KB · Views: 405
John H thanks you so much. Your post solved my problem completely, the rest work is mine since you have made foundation for. I love you!

Remain blessed!
 
John H your code solved my problem remaining that it rotates like clock. It ticks, can u help give me crew on how to make rotate like tyre of a car or electric fan not like wall clock just like encarta 2004

Plz ur help is of immense importance.

God bless
 
JohnH Thank you for that code please assist me in reversing the direction

See the attached sample, I haven't seen Encarta but added 6 Pictureboxes to a form in a circular fashion and used GraphicsPath to get the point references of this circle, then used BackgroundWorker to "animate" them by setting new locations not too fast. I just wrote the name of the Picturebox so it is easy to see which is which when they move around, didn't load any images. This should give you an idea of what you can work with.


Thank you very much for that code JohnH that really helped me allot please could you help me in reversing the direction in which the picturebox's turn i need them to go to the left and not the right.
I need them to turn anti clockwise please assist me man.
 
reversing the direction
In Initialize method add (after loop and before AddClosedCurve call)
pts.Reverse()
 
Thanks JohnH

In Initialize method add (after loop and before AddClosedCurve call)
pts.Reverse()


Thanks you so much JohnH, you're a legend bud. Thanks
 
Hi JohnH I wonder if I could asked you one last thing please

In Initialize method add (after loop and before AddClosedCurve call)
pts.Reverse()

The pts.reversed() works great thank you, but do you have any idea how i could use both at the same time.
In other words have a Clockwise button that turns it clockwise and then a anti clockwise button that turns it anticlockwise on the same form

I tried to modify it to do this but it seems once the path is drawn it will only go in that one direction
and cannot go in both directions on the same form.

I tried to make two initialize2() methods and two background workers for the anti clockwise button, but which was a mess. I just cannot seem to figure it out.
 
The Rotate method works on the path.PathPoints array going in one direction (i -= 1). You could rework that code to reverse the direction too.
 
Not sure if I am going about it the right way but i have got it to work by adding a second path for anti clockwise

==================================================

Public Class Form1


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
initialize1()
initialize2()
End Sub


Private boxes1 As New List(Of PictureBox)
Private path1 As New Drawing2D.GraphicsPath


Dim pts1 As New List(Of Point)
Private Sub initialize1()
'get pictureboxes into a list and their locations into an array


For Each c As Control In Me.Controls
If TypeOf c Is PictureBox Then
boxes1.Add(c)
pts1.Add(c.Location)
End If
Next


'use GraphicsPath to add the location points
'in this case this will result in a nice circle with 2 point between each picturebox
path1.AddClosedCurve(pts1.ToArray)
'there are now 19 points in path.PathPoints array, first equals last (closed figure)


'get the index of the picturebox location point in the path.PathPoints
'and set it in the Tag of the box for later reference
Dim pb As PictureBox
For p As Integer = 0 To boxes1.Count - 1
pb = boxes1(p)
For i As Integer = 1 To path1.PathPoints.Length - 1
If pb.Location = path1.PathPoints(i) Then
pb.Tag = i
End If
Next
Next
End Sub


Private boxes2 As New List(Of PictureBox)
Private path2 As New Drawing2D.GraphicsPath


Dim pts2 As New List(Of Point)
Private Sub initialize2()
'get pictureboxes into a list and their locations into an array




For Each c As Control In Me.Controls
If TypeOf c Is PictureBox Then
boxes2.Add(c)
pts2.Add(c.Location)
End If
Next
pts2.Reverse()
'use GraphicsPath to add the location points
'in this case this will result in a nice circle with 2 point between each picturebox
path2.AddClosedCurve(pts2.ToArray)
'there are now 19 points in path.PathPoints array, first equals last (closed figure)


'get the index of the picturebox location point in the path.PathPoints
'and set it in the Tag of the box for later reference
Dim pb As PictureBox
For p As Integer = 0 To boxes1.Count - 1
pb = boxes2(p)
For i As Integer = 1 To path2.PathPoints.Length - 1
If pb.Location = path2.PathPoints(i) Then
pb.Tag = i
End If
Next
Next


End Sub


' un-comment this to see the path drawn on form:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawPath(Pens.Blue, path1)
e.Graphics.DrawPath(Pens.Red, path2)
End Sub


'just to see which box is which, draws the name
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint, PictureBox6.Paint, PictureBox5.Paint, PictureBox4.Paint, PictureBox3.Paint, PictureBox2.Paint
Dim pb As PictureBox = sender
e.Graphics.DrawString(pb.Name, Me.Font, Brushes.Blue, 0, 0)
End Sub




Private Sub btnRotateclock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRotateclock.Click
'animation handled by the BackgroundWorker asynchronous in order to not sleep UI thread
If Not BackgroundWorker1.IsBusy Then
BackgroundWorker1.RunWorkerAsync()
End If
End Sub


Private Sub btnRotateanticlock_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRotateanticlock.Click
'animation handled by the BackgroundWorker asynchronous in order to not sleep UI thread
If Not BackgroundWorker2.IsBusy Then
BackgroundWorker2.RunWorkerAsync()
End If
End Sub




Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'rotate each picturebox three steps in the path.PathPoints array.
'this will move each box to the next main location point
For x As Integer = 0 To 2
BackgroundWorker1.ReportProgress(0) 'report to UI thread
If x < 2 Then Threading.Thread.Sleep(200) 'sleep thread 200 milliseconds between each step
Next
End Sub


Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
rotate1()
End Sub


Private Sub BackgroundWorker2_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
'rotate each picturebox three steps in the path.PathPoints array.
'this will move each box to the next main location point
For x As Integer = 0 To 2
BackgroundWorker2.ReportProgress(0) 'report to UI thread
If x < 2 Then Threading.Thread.Sleep(200) 'sleep thread 200 milliseconds between each step
Next
End Sub


Private Sub BackgroundWorker2_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker2.ProgressChanged
rotate2()
End Sub


Private Sub rotate1()
'rotates each picturebox one step in the path.PathPoints array
Dim i As Integer
For Each pb As PictureBox In boxes1
i = pb.Tag
i -= 1
If i = 0 Then i = path1.PathPoints.Length - 1
pb.Location = Point.Round(path1.PathPoints(i))
pb.Tag = i
pb.Refresh()
Next
End Sub


Private Sub rotate2()
'rotates each picturebox one step in the path.PathPoints array
Dim i As Integer
For Each pb As PictureBox In boxes2
i = pb.Tag
i -= 1
If i = 0 Then i = path2.PathPoints.Length - 1
pb.Location = Point.Round(path2.PathPoints(i))
pb.Tag = i
pb.Refresh()
Next
End Sub


End Class

=============================================================

But of course now the problem is when you rotate clockwise a few times then rotate anticlockwise
the pictureboxs are not in the correct places as they move to the starting point of path2 instead of moving backward from where
path1 has left off

I'm trying to say something like this on btnrotateanticlockwise button
path2.current.pathpoints = path1.current.pathpoints
have not figured this out yet ... not sure if I'm going about it the right way but ill keep playing with it
 
Back
Top