玄関へお回り下さい。
直線・四角形・楕円・三角形(多角形)を描画及び消去 (11個)          (SNo.092)

1.Form に直線(実線・点線・矢印)を描画
2.PictureBox に直線(実線・点線・矢印)を描画
3.Form と PictureBox に四角形・楕円・円を描画
4.Form と PictureBox に三角形(多角形)を描画
5.描画したものを消す
使用コントロール Button1  Button2  Button3  Button4  Button5 PictureBox1
その他条件 WindowsXP(Vista) Visual Basic 2005(VB2008)
 
1.Form に直線(実線・点線・矢印)を描画
 
Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click
'Formに直線を描画
    Me.CreateGraphics.DrawLine(New Pen(Color.Black, 2), 10, 20, 170, 20)

    '又は

    Dim g As Graphics = Me.CreateGraphics
    Dim BPen As New Pen(Color.Black, 3)         'ラインの色・ラインの太さを設定
    BPen.DashStyle = Drawing2D.DashStyle.Dash   '線種を点線に
    Dim pt1 As New Point(10, 30)                'ラインの描画開始地点
    Dim pt2 As New Point(170, 30)               'ラインの描画終了地点
    g.DrawLine(BPen, pt1, pt2)                  'フォームにラインを描画
    BPen.Dispose()
    g.Dispose()
End Sub
 
2.PictureBox に直線(実線・点線・矢印)を描画
 
Private Sub Button2_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button2.Click
'PictureBoxに直線を描画
    PictureBox1.CreateGraphics.DrawLine( _
                        New Pen(Color.Black, 2), 10, 20, 170, 20)

    '又は

    Dim g As Graphics = PictureBox1.CreateGraphics
    Dim BPen As New Pen(Color.Black, 5)         'ラインの色・ラインの太さを設定
    BPen.EndCap = Drawing2D.LineCap.ArrowAnchor 'ラインの先端を矢印に
    Dim pt1 As New Point(10, 30)                'ラインの描画開始地点
    Dim pt2 As New Point(170, 30)               'ラインの描画終了地点
    g.DrawLine(BPen, pt1, pt2)                  'フォームにラインを描画
    BPen.Dispose()
    g.Dispose()
End Sub
 
3.Form と PictureBox に四角形・楕円・円を描画
 
Private Sub Button3_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button3.Click
'四角形と円形を描画
    '四角形を描く
    Me.CreateGraphics.DrawRectangle(New Pen(Color.Black, 2), 10, 40, 100, 40)
    '円形を描く
    Me.CreateGraphics.DrawEllipse(New Pen(Color.Black, 2), 120, 40, 40, 40)

    '四角形を描く
    PictureBox1.CreateGraphics.DrawRectangle( _
                            New Pen(Color.Black, 2), 10, 40, 100, 40)
    '楕円形を描く
    PictureBox1.CreateGraphics.DrawEllipse( _
                            New Pen(Color.Black, 2), 120, 40, 55, 40)
End Sub
 
4.Form と PictureBox に三角形(多角形)を描画
 
Private Sub Button4_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button4.Click
'三角形(多角形)を描画
    '三角形(多角形)を描画
    Me.CreateGraphics.DrawPolygon(New Pen(Color.Black, 2), New Point() _
            {New Point(200, 10), New Point(290, 100), New Point(200, 100)})

    Dim g As Graphics = PictureBox1.CreateGraphics
    Dim BPen As New Pen(Color.Black, 1)     'ラインの色・ラインの太さを設定
    Dim pt1 As New Point(200, 10)           'ラインの描画開始地点
    Dim pt2 As New Point(290, 100)          'ラインの次の地点
    Dim pt3 As New Point(200, 100)          'ラインの描画終了地点
    Dim pos As Point() = {pt1, pt2, pt3}    '各接点の座標の配列
    g.DrawPolygon(BPen, pos)                '配列で定義した多角形を描画
    BPen.Dispose()
    g.Dispose()
End Sub
 
5.描画したものを消す
 
Private Sub Button5_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button5.Click
   
Me.CreateGraphics.Clear(Me.BackColor)
   
PictureBox1.CreateGraphics.Clear(PictureBox1.BackColor)
End Sub
部分的に消すには、描いた物と同じ物をバックカラーで再度描けば消える事になります。
        結果
      
基本的な事が解れば、ヘルプでGraphics メンバーを調べてみれば色々な描画ができます。
又、三角形の接点の数を増やせば色んな形の図形が描けます。





2004/08/27
2005/10/13


VBレスキュー(花ちゃん)
VB.NET2003  VB2005