刷新panel而不是form

版权声明: https://blog.csdn.net/eds124/article/details/82557283
Public Class Form1
    Dim WithEvents p As Panel = New Panel()
    Private ya As Integer = 500, xa As Integer = 500, ky As Integer = 1000, x As Integer = 1, y As Integer = 1
    '  Private i2(,) As Integer = {{0, 0, 0}, {1, 0, 1}, {0, 0, 0}, {1, 0, 1}}
    Private i2(,) As Integer = {{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 2, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}, {0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 2, 3, 0, 2, 2, 23, 4, 32, 1, 23, 23, 2, 23, 4, 3, 2, 3, 343}}

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Controls.Add(p)
        Me.WindowState = FormWindowState.Maximized
        p.BackColor = Color.Black
        p.Location = New Point(10, 10)
        p.Size = New Size(400, 300)
    End Sub

    Public Sub p_Paint(ByVal o As Object, ByVal pea As PaintEventArgs) Handles p.Paint
        Dim g As Graphics = pea.Graphics

        For i As Integer = 0 To i2.GetLength(0) - 1
            For j As Integer = 0 To i2.GetLength(1) - 1
                If i2(i, j) = 0 Then
                    g.FillRectangle(Brushes.Blue, j * 20, i * 20, 20, 20)
                ElseIf i2(i, j) = 1 Then
                    g.FillRectangle(Brushes.Green, j * 20, i * 20, 20, 20)
                ElseIf i2(i, j) = 2 Then
                    g.FillRectangle(Brushes.Yellow, j * 20, i * 20, 20, 20)
                ElseIf i2(i, j) = 3 Then
                    g.FillRectangle(Brushes.Pink, j * 20, i * 20, 20, 20)
                Else
                    g.FillRectangle(Brushes.Orange, j * 20, i * 20, 20, 20)
                End If
            Next
        Next
        g.FillRectangle(Brushes.Red, xa, ya, 100, 100)

    End Sub

    Public Sub p_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Dim k As Keys = e.KeyCode
        If Keys.E = k And ya > 0 Then
            ya -= 100
        ElseIf Keys.D = k And ya < 900 Then
            ya += 100
        ElseIf Keys.S = k And xa > 0 Then
            xa -= 100
        ElseIf Keys.F = k And xa < 1800 Then
            xa += 100
        ElseIf Keys.K = k And ky > 800 Then
            ky -= 200
        ElseIf Keys.J = k And ky < 1000 Then
            ky += 200
        End If
        p.Refresh()
    End Sub
End Class

猜你喜欢

转载自blog.csdn.net/eds124/article/details/82557283