重新绘制表单时,我应该使用me.invalidate还是me.refresh?

时间:2016-06-17 01:19:13

标签: vb.net graphics repaint

我正在Windows Form尝试绘画事件,我想要实现的是重新绘制箭头的颜色。我想我成功地做到了这一点,我只是在表单上执行重绘事件时是否应该使用Me.InvalidateMe.Refresh?他们两个都在工作。

请注意,我稍后会创建许多对象,因此我的目标之一是减少性能问题。

当您点击按钮1时,它会使用line 1Me.Invalidate的颜色更改为黄色,当您点击按钮2时,它会将line 1更改为蓝色Me.Refresh。他们两个都在工作。

这是我的全部代码:

Public Class Form1

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
        drawLeftArrow(e, "a", 100, 250, 400, 50)
    End Sub

    Dim aPen As Pen = New Pen(Color.Red, 8)
    Dim bPen As Pen
    Dim cPen As Pen

    Dim line1 As Point()
    Dim line2 As Point()
    Dim line3 As Point()

    Sub drawLeftArrow(e As PaintEventArgs, letterPosition As String, startPointX As Integer, startPointY As Integer, endPointX As Integer, endPointY As Integer)

        bPen = New Pen(Color.Green, 8)
        cPen = New Pen(Color.Yellow, 8)

        Dim mycap As Drawing2D.CustomLineCap = New Drawing2D.AdjustableArrowCap(5, 5)

        ' Create points that define curve.
        Dim startPoint As New Point(startPointX, startPointY)



        Dim endPoint As New Point(endPointX, endPointY)


        Select Case letterPosition
            Case "a"
                Dim middlePoint1 As New Point(startPointX + 250, startPointY)
                Dim middlePoint2 As New Point(startPointX + 300, endPointY + 150)
                line1 = {startPoint, middlePoint1}
                line2 = {middlePoint1, middlePoint2}
                line3 = {middlePoint2, endPoint}
                cPen.CustomEndCap = mycap
                ' Draw lines between original points to screen.
                e.Graphics.DrawLines(aPen, line1)
                e.Graphics.DrawLines(bPen, line2)
                e.Graphics.DrawLines(cPen, line3)
                Exit Select
        End Select

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        aPen = New Pen(Color.Yellow, 8)
        Me.Invalidate()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        aPen = New Pen(Color.Blue, 8)
        Me.Refresh()
    End Sub
End class

输出:

enter image description here

0 个答案:

没有答案