Visual Basic 中学校 > Visual Basic サンプル集 > Visual Basic サンプル集目次 >
グラフィックスが消えないようにする
VBでFormやPictureBoxなどに出力したグラフィックスは、自動的に再描画されない。ここでは自動的に再描画される方法を説明する。
1.あらかじめ自動的に再描画されるようにする例
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim g As Graphics = AutoGraphics(PictureBox1)
g.Clear(Color.Black)
g.FillPie(Brushes.Aqua, 10, 10, 100, 100, 0, 90)
End Sub
Public Function AutoGraphics(ByVal picSource As PictureBox) As Graphics If picSource.Image Is Nothing Then
picSource.Image = New Bitmap(picSource.ClientRectangle.Width, picSource.ClientRectangle.Height)
End If
Return Graphics.FromImage(picSource.Image)
End Function
メモ:この例で描画したグラフィックスを消去するには次のようにします。
PictureBox1.Image = Nothing
2.再描画が必要なタイミングで毎回再描画する例
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint e.Graphics.Clear(Color.Black)
e.Graphics.FillPie(Brushes.Aqua, 10, 10, 100, 100, 0, 90)
End Sub
メモ:再描画が必要なタイミングでは必ずPaintイベントが発生することを利用する。