我们不能在 VB.NET 中第二次初始化一个 Font 吗?

Can not we initialize a Font for second time in VB.NET?

我正在 VB.NET 制作 2D 游戏。我只使用一种字体对象在表单上绘制字符串。此字体仅在 游戏菜单 中需要。因此,我在不需要时处理字体,并在需要时重新初始化它。

font_1 = New Font("Autobus Bold", 15.0)

当我使用此字体 (font_1) 在表单上绘制字符串时,出现此错误。

An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll

Additional information: Parameter is not valid.

当我查看字体时,它显示,

{Name = Reference to a non-shared member requires an object reference. Size=15.0}

第一次加载游戏菜单时(font_1第一次初始化时不会出现此错误)。当用户玩游戏时,字体会被处理。当用户再次进入游戏菜单时,字体在用于绘图之前会再次初始化。在window上使用字体绘制字符串时,会出现此错误。

看起来错误只出现在字体系列中。我在几个论坛上看到这个问题,但没有人给出解决方案。 (这是我在论坛上的第一个问题)

已编辑:我删除了字体 (font_1)。但我仍然遇到同样的错误。这是绘制字符串的代码。

Private Sub mcFramesHandler_TIMER_Tick(sender As Object, e As EventArgs) Handles mcFramesHandler_TIMER.Tick

    gB.Clear(Color.Black)
    gB.DrawImage(Background_IMG, 0, 0, 640, 480)

    Select Case currentMode

        Case GameMode.OnGame
            If mcShoot_TIMER.Enabled Then gB.DrawImage(Bullet_IMG, Bullet_X, Bullet_Y, 20, 50)
            If mcEneShoot_TIMER.Enabled Then gB.DrawImage(EneBullet_IMG, EneBullet_X, EneBullet_Y, 20, 50)
            If Shooter_Lives Then gB.DrawImage(Shooter_IMG, Shooter_X, Bullet_Y_Def, 100, 105)
            If mcMoveEnemy_TIMER.Enabled Then gB.DrawImage(Enemy_IMG, Enemy_X, 10, 100, 80)
            If mcExplode_TMER.Enabled Then gB.DrawImage(Explotion_IMG, Explotion_X, Explotion_Y, 100, 80)

        Case GameMode.Begining
            gB.DrawString("Start", New Font("Autobus Bold", 15.0), textBrush(0), 110, 98) 'Error is generated in this line
            gB.DrawString("Credits", New Font("Autobus Bold", 15.0), textBrush(1), 102, 158)
            gB.DrawString("Exit", New Font("Autobus Bold", 15.0), textBrush(2), 114, 218)

    End Select

    Me.CreateGraphics.DrawImage(backbuffer, 0, 0, 640, 480)

End Sub

这里textBrudh(0)是画笔。 gB 是图形对象。 gB 在绘制字符串之前成功绘制了背景图像。这仅在显示 游戏菜单 时发生 非常感谢您的支持。

完成绘制后,您需要围绕 Graphics 放置所有对象,不仅是字体,还有所有对象(最好使用如下代码中的闭包技术)

如果您需要一个新的 Graphics 实例,您可以在需要时创建它,而无需使用全局对象(当您使用 Graphics 时)。

     Using gB As Graphics = Me.CreateGraphics

        Using textBrush As Brush = Brushes.Black

            Using font_1 As Font = New Font("Courier New", 15)

                gB.DrawString("Start", font_1, textBrush, 400, 98)

                'gB.DrawOtherthings()
                'gB.DrawOtherthings()
                'gB.DrawOtherthings()
                'gB.DrawOtherthings()
                'gB.DrawOtherthings()
                'gB.DrawOtherthings()
                'gB.DrawOtherthings()
                'gB.DrawOtherthings()
                'gB.DrawOtherthings()

            End Using

        End Using

    End Using

画笔有问题(textBrush:画笔数组)。当我处理数组中的每个项目并重新初始化时,看起来它们发生了一些变化(好像它们不再是刷子了。再次查看错误。它说 "Parameter is not valid")。所以我只是将数组重新调整为 0.

Redim textBrush (0)

这会清除以前的项目。然后我重新调整数组,在需要时再次初始化它们中的每一个。

Redim textBrush (2)
textBrush (0) = Brushes.Yellow
textBrush (1) = Brushes.Red
textBrush (2) = Brushes.Red

该数组用于在按下上下键时改变文本颜色。 我看到很多人在论坛上提到这个错误

{Name = Reference to a non-shared member requires an object reference. Size=15.0}

我不知道这个名字是什么,但这不影响节目。在任何字体中都是如此。