【问题标题】:vb.net DrawArc won't refresh in Pictureboxvb.net DrawArc不会在Picturebox中刷新
【发布时间】:2016-03-30 05:19:05
【问题描述】:

我正在尝试在图片框中显示正在绘制的动画弧。我无法在每个循环(每 100 度)处绘制弧线。油漆仅在子程序结束时触发,最终弧度为 300 度。如何在每个循环期间强制刷新图片框?这是我的代码和表单。

 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

我尝试在循环中使用picturebox1.refresh()、update()、invalidate(),但没有成功。

【问题讨论】:

  • 您可以尝试将Application.DoEvents(); 放在Loop 末尾之前
  • DoEvents 不起作用
  • 问题是您使用的是 e.Graphics。在引发Paint 事件的代码返回之前,这不是进程。不确定,但如果您从表单中获得 Graphics,它可能会像您预期的那样工作。
  • 你是对的,功能在表单级别起作用。但是,我计划进行快速重绘,并且不想重绘整个表单。我在表单级别看到一些图形闪烁。我希望只重绘图片框,以尽量减少闪烁和更快的图形重绘。
  • PictureBox 是双缓冲的,在您的 Paint 事件处理程序返回之前您不会看到结果。永远,永远永远挂起一个 Paint 事件,永远。改用 Timer,在 Tick 事件处理程序中调用 Invalidate() 来强制重绘。

标签: vb.net drawing picturebox


【解决方案1】:

如果你改用计时器会怎样?

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

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 2018-06-04
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多