vb.net - 导致纯白色按钮的图形
问题描述
我正在编写康威的人生游戏。我的网格完全在图片框中,但是当我加载表单时,右上角的后退按钮完全是白色的。刷新表单会修复按钮,但会使其非常滞后。每个其他按钮都显示得很好,只是后退按钮坏了。我怎样才能解决这个问题?
Option Strict On
Public Class frmGame
' Declaring public variables
Public bmp As Bitmap
Public G As Graphics
Public WithEvents speed As Timer
Public grid(50, 40) As Boolean
Public input(50, 40) As Boolean
Public intGens As Integer = 0
Public change As Boolean = False
Public P As New Pen(Color.Black)
Private Sub picGrid_Paint(sender As Object, e As PaintEventArgs) Handles picGrid.Paint
' Loads bitmap, graphics, etc and prepares to begin simulation
' Creates graphics device
bmp = New Bitmap(600, 480)[enter image description here][1]
picGrid.Image = bmp
G = Graphics.FromImage(bmp)
' Defining variables for grid
Dim x As Integer = 0
Dim y As Integer = 0
' Draws grid
For y = 0 To 480
For x = 0 To 600
G.DrawRectangle(pen:=P, x:=x, y:=y, width:=12, height:=12)
x += 12
Next
x = 0
y += 12
Next
End Sub
Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
' Hides rules form, shows main menu form
frmMainMenu.Show()
Me.Hide()
End Sub
Private Sub frmGame_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
' Frees memory when form is closed
G.Dispose()
bmp.Dispose()
speed.Dispose()
End Sub
End Class
解决方案
我不确定我是否理解您的问题,也不能使用提供的代码重新创建它。但我可以推荐一些可以帮助你的东西。
首先,在处理绘图时不要使用 Control.Refresh(),而是使用 Control.Invalidate() 和 Control.Update(),这可能会解决滞后问题。
您是否需要 2 个表格(我理解这是问题的根源)?不要隐藏表单,而是隐藏 PictureBox (.Visible = False) 并显示您当时需要的其他控件。