图形在滚动时在面板中消失

时间:2016-01-09 18:10:11

标签: vb.net system.drawing

我有代码

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint

    Using myPen As New Pen(Drawing.Color.BurlyWood, 6)


        If upTimer.Enabled = True Then

            e.Graphics.DrawLine(myPen, x1, y1, x1, y2)

        End If
        If leftTimer.Enabled = True Then
            e.Graphics.DrawLine(myPen, x1, y1, x2, y1)
        End If
        If rightTimer.Enabled = True Then
            e.Graphics.DrawLine(myPen, x1, y1, x2, y1)
        End If
        If downTimer.Enabled = True Then

            e.Graphics.DrawLine(myPen, x1, y1, x1, y2)
        End If
    End Using
End Sub

其中x1,y1,x2和y2在form_load中被初始化为零。

计时器代码是:

  Private Sub upTimer_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick

    y2 = y2 + 5

End Sub
 Private Sub leftTimer_Tick(sender As Object, e As EventArgs) Handles leftTimer.Tick
    x2 = x2 - 5 

End Sub
Private Sub rightTimer_Tick(sender As Object, e As EventArgs) Handles rightTimer.Tick

    x2 = x2 + 5
End Sub
 Private Sub downTimer_Tick(sender As Object, e As EventArgs) Handles downTimer.Tick

    y2 = y2 - 5 
End Sub

我有四个按钮来启用这四个计时器。现在的问题是当我自动或手动滚动包含在面板中的图片框时,图纸会消失。我怎么能避免这个呢?

1 个答案:

答案 0 :(得分:0)

尝试使用此解决方法:

Private Sub Panel1_Scroll(sender As Object, e As ScrollEventArgs) Handles Panel1.Scroll
   PictureBox1.Refresh()
End Sub

但是当您滚动面板时,图片框必须自行刷新。我通过一个简单的测试尝试了这个:我在picturebox的“paint”事件中添加了一个全局变量“count”然后我将+1添加到“count”变量并将此值(从paint事件)打印到一个标签中形成。每个卷轴都必须包含计数器。