2015-12-23 71 views
1

我想顯示一個正在畫框中繪製的動畫弧。我無法在每個循環(每100度)繪製弧線。油漆僅在子程序結束時觸發,最終電弧爲300度。如何在每個循環中強制刷新圖片框?這是我的代碼和表單。vb.net DrawArc不會在Picturebox中刷新

Private Sub PictureBox1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint 

    Dim pen As New Pen(Color.Red, 1) 
    Dim r As Integer = 100 

    'Delay(2) 

    Do Until r > 300 
     e.Graphics.DrawArc(pen, 50, 50, 50, 50, 270, r)   ' pen style, x position, y postion, width, height, start point degrees, arc degrees 
     ListBox1.Items.Add(r) 
     ListBox1.SelectedIndex = ListBox1.Items.Count - 1 
     r = r + 100 
     Delay(1) 
    Loop 

    e.Dispose() 
    pen.Dispose() 
    ListBox1.Items.Add("Done") 

End Sub 

enter image description here

我使用picturebox1.refresh(),更新(),無效()沒有成功的循環內進行審判。

+0

你可以嘗試把'Application.DoEvents();'只是你'Loop' – Darren

+0

的DoEvents年底前沒有工作 – HurstOlds

+0

的問題是,你正在使用e.Graphics。直到引發'Paint'事件的代碼返回後,纔會執行這些操作。不確定,但如果您從表單中獲取'Graphics',它可能會像您期望的那樣工作。 – Steve

回答

2

如果您使用計時器而不是?

Imports System.Timers.Timer 

Public Class Form1 
Dim tmr1 As New Timer 
Dim r As Int32 = 0 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 
    Me.DoubleBuffered = True 

    AddHandler tmr1.Tick, AddressOf tmr1_Tick 
    With tmr1 
     .Interval = 1000 
     .Start() 
    End With 
End Sub 

Private Sub tmr1_Tick(ByVal sender As Object, ByVal e As EventArgs) 
    If r < 300 Then 
     r += 100 
     ListBox1.Items.Add(r) 
     ListBox1.SelectedIndex = ListBox1.Items.Count - 1 
     Me.PictureBox1.Invalidate() 
    Else 
     ListBox1.Items.Add("Done") 
     tmr1.Stop() 
    End If 
End Sub 

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint 
    Dim pen As New Pen(Color.Red, 1) 
    Dim g As Graphics = e.Graphics 
    Debug.WriteLine("PictureBox1: " & r) 
    g.DrawArc(pen, 50, 50, 50, 50, 270, r)   ' pen style, x position, y postion, width, height, start point degrees, arc degrees 

    pen.Dispose() 
End Sub 
End Class 
+0

完美的解決方案...謝謝! – HurstOlds