0

我正在为一个类项目在 Visual Basic 中创建一个刽子手游戏,并且我创建了一个函数,当您在单词中的适当位置猜出正确的字母时,它会绘制一个字符串。我的问题是,每当我做出新的猜测时,它都会删除旧图形并添加新图形。我需要一种方法来保存我绘制的图形,有人知道吗?

Dim newCharArray() As Char = wordArray(rndNumber).ToCharArray
For i As Integer = 0 To (wordArray(rndNumber).Length - 1) Step 1
    If TextBox1.Text = newCharArray(i) Then
        correctLetter = newCharArray(i)
        letterPos = i
        Me.Refresh()
    End If
Next

Dim drawFont As New Font("Microsoft YaHei UI", 16)
Dim drawBrush As New SolidBrush(Color.Black)
Dim drawString As [String] = CStr(correctLetter)
Dim drawPoint As New PointF(x:=(x1 + 4) + (letterPos * 40), y:=363)
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint)
4

1 回答 1

0

问题是Paint方法负责每次从头开始绘制整个内容。

Paint我会维护所有正确字符及其位置的列表,并在方法中每次都绘制它们。

或者您可以将每个字符绘制为 a Bitmap,然后在Paint方法中绘制位图,如下所示:

Private canvas As Bitmap = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
...

    Dim newCharArray() As Char = wordArray(rndNumber).ToCharArray
    For i As Integer = 0 To (wordArray(rndNumber).Length - 1) Step 1
        If TextBox1.Text = newCharArray(i) Then
            correctLetter = newCharArray(i)
            letterPos = i
            Me.Refresh()
        End If
    Next
...

Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

    Dim drawFont As New Font("Microsoft YaHei UI", 16)
    Dim drawBrush As New SolidBrush(Color.Black)
    Dim drawString As [String] = CStr(correctLetter)
    Dim drawPoint As New PointF(x:=(x1 + 4) + (letterPos * 40), y:=363)

    Using g As Graphics = Graphics.FromImage(Me.canvas)
        g.DrawString(drawString, drawFont, drawBrush, drawPoint)
    End Using

    e.Graphics.DrawImage(Me.canvas, New Point(0, 0))
End Sub
于 2013-10-17T10:51:02.573 回答