delete the lines in a form after draw it

quangha

Member
Joined
Jan 30, 2010
Messages
16
Programming Experience
Beginner
i tried to make an animation effect ( paper flip) using timer. everything works fine but i don't know how to delete the remaining lines.
This is the result
Capture-2.png

and here is the code
VB.NET:
Imports System.Drawing
Imports System.Drawing.Drawing2D
Public Class Form1
    Dim i As Integer
    Dim count As Integer = 40
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim g As Graphics = Me.CreateGraphics
        Dim pen As Pen = New Pen(Color.Green, 2)
        Timer1.Interval = 10
        Timer1.Start()
        ve()
    End Sub
    Sub ve()
        Dim g As Graphics = Me.CreateGraphics
        Dim pen As Pen = New Pen(Color.Green, 2)
        Dim p1, p2, p3, p4 As Point
        Dim p1x, p1xa, p1y, p1ya, p2x, p2xa, p2y, p2ya, p3x, p3y, p4x, p4y As Integer
        p1x = count
        p1xa = count - 190
        p1ya = (1 / 15) * System.Math.Sqrt(150 * 150 - p1xa * p1xa)
        p1y = 40 - p1ya
        p2xa = -p1xa
        p2x = 190 + p2xa
        p2ya = -p1ya
        p2y = 40 + p1ya
        p3x = p2x
        p3y = p2y + (200 - 2 * System.Math.Abs(p2ya))
        p4x = p1x
        p4y = p1y + 200 + 2 * p1ya
        p1 = New Point(p1x, p1y)
        p2 = New Point(p2x, p2y)
        p3 = New Point(p3x, p3y)
        p4 = New Point(p4x, p4y)
        Dim pts() As Point = {p1, p2, p3, p4}
        g.DrawPolygon(pen, pts)

    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ve()
        count = count + 5
        If count = 340 Then
            Timer1.Stop()
        End If
    End Sub

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

    End Sub
End Class
 
One poss way is to create a List(Of T) holding all your lines(points), I would use a class object for storing them, and in your paint event iterate the List and rearrange the points and then repaint them. To remove one is now as easy as using the Remove method.
 
@newguy: THanks alot. But i'm a totally beginner in programming and GDI+ , can you give me the code ?
I'm trying to make a card flip effect, the front and back of the card's content is taken from a database, so, i think this kind of graphic can't handle that, anyone, HELPPPP !!!
 
I think what you are looking for is; clear the object info you are displaying, draw each line and with a thread.Sleep(50) <- or other interval B/T each drawline() move effect, then remove each line from first to last then display the new object info. I am at work, but will see if I can give you and example line class where you add them/remove them. I am also thinking of ControlPaint.DrawReversibleLine.
 
Something to work on...
VB.NET:
Public Class Form1
  Dim count As Integer = 40
  Dim polyList As New List(Of poly)
  Dim isUndoing As Boolean
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer2.Enabled = True
  End Sub
  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    If Not isUndoing Then
      Dim g As Graphics = e.Graphics
      For Each p As poly In polyList
        Dim pp As Point() = {p.p1, p.p2, p.p3, p.p4}
        g.DrawPolygon(Pens.Navy, pp)
      Next
      g.Dispose()
    Else
      Dim g As Graphics = e.Graphics
      For i As Integer = polyList.Count - 1 To 0 Step -1
        Dim pp As Point() = {polyList.Item(i).p1, polyList.Item(i).p2, polyList.Item(i).p3, polyList.Item(i).p4}
        g.DrawPolygon(Pens.Navy, pp)
      Next
      g.Dispose()
    End If
  End Sub
  Private Sub ve()
    Dim pen As Pen = New Pen(Color.Navy, 2)
    Dim p1, p2, p3, p4 As Point
    Dim p1x, p1xa, p1y, p1ya, p2x, p2xa, p2y, p2ya, p3x, p3y, p4x, p4y As Integer
    p1x = count
    p1xa = count - 190
    p1ya = (1 / 15) * System.Math.Sqrt(150 * 150 - p1xa * p1xa)
    p1y = 40 - p1ya
    p2xa = -p1xa
    p2x = 190 + p2xa
    p2ya = -p1ya
    p2y = 40 + p1ya
    p3x = p2x
    p3y = p2y + (200 - 2 * System.Math.Abs(p2ya))
    p4x = p1x
    p4y = p1y + 200 + 2 * p1ya
    p1 = New Point(p1x, p1y)
    p2 = New Point(p2x, p2y)
    p3 = New Point(p3x, p3y)
    p4 = New Point(p4x, p4y)
    Dim pts() As Point = {p1, p2, p3, p4}
    Dim polyG As New poly
    polyG.p1 = p1
    polyG.p2 = p2
    polyG.p3 = p3
    polyG.p4 = p4
    polyList.Add(polyG)
    count += 5
    Me.Invalidate()
  End Sub
  ' erases
  Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If polyList.Count = 1 Then
      Timer1.Enabled = False
      Exit Sub
    End If
    polyList.Remove(polyList.Item(0))
    Me.Invalidate()
  End Sub
  'adds
  Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    If Not count = 340 Then
      ve()
    Else
      count = 40
      Timer1.Enabled = True
      isUndoing = True
      Timer2.Enabled = False
    End If
  End Sub
  Public Class poly
    Public p1 As Point
    Public p2 As Point
    Public p3 As Point
    Public p4 As Point
  End Class
End Class
 
Thank you very much, newguy. I can managed to make the animation :) . Not so professional but acceptable. Again, thanks alot for your help
 
I do all my drawing on a picturebox and use it's Paint event, so not sure if that would help.
 
Back
Top