ヘッダー
Visual Basic サンプル集
VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

直線を描画する

2020/11/22

→ C# のサンプルに切り替える

 

このページで紹介するサンプルは Windowsフォームアプリケーションを前提にしています。

描画するタイミングについては下記にサンプルを参照してください。

ボタンをクリックしたら図形を描画する

マウス操作で図形を描画する

 

PictureBoxに赤い線を描画する

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim point1 As New Point(10, 20)
    Dim point2 As New Point(200, 150)

    e.Graphics.DrawLine(Pens.Red, point1, point2)
End Sub

 

 

Formに青い線を描画する

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    Dim point1 As New Point(10, 20)
    Dim point2 As New Point(200, 150)

    e.Graphics.DrawLine(Pens.Blue, point1, point2)
End Sub

 

 

太さを指定して線を描画する

次の例は太さ12の赤い線を描画します。

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim point1 As New Point(100, 10)
    Dim point2 As New Point(310, 10)

    Dim pen As New Pen(Color.Red, 12)

    e.Graphics.DrawLine(pen, point1, point2)
End Sub

 

 

点線を描画する

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim point1 As New Point(100, 10)
    Dim point2 As New Point(310, 10)

    Dim pen As New Pen(Color.Red)
    pen.DashStyle = Drawing2D.DashStyle.Dot

    e.Graphics.DrawLine(pen, point1, point2)
End Sub

 

次の例は、PenのDashStyleを変更することでさまざまな点線・破線を描画します。

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint

    Dim font As New Font("Verdana", 20) 'Verdanaで大きさ20のフォント
    Dim redPen As New Pen(Color.Red, 4) '太さ4の赤いペン

    e.Graphics.DrawString("Solid", font, Brushes.Black, 10, 0)
    redPen.DashStyle = Drawing2D.DashStyle.Solid
    e.Graphics.DrawLine(redPen, 190, 20, 400, 20)

    e.Graphics.DrawString("Dash", font, Brushes.Black, 10, 40)
    redPen.DashStyle = Drawing2D.DashStyle.Dash
    e.Graphics.DrawLine(redPen, 190, 60, 400, 60)

    e.Graphics.DrawString("Dot", font, Brushes.Black, 10, 80)
    redPen.DashStyle = Drawing2D.DashStyle.Dot
    e.Graphics.DrawLine(redPen, 190, 100, 400, 100)

    e.Graphics.DrawString("DashDot", font, Brushes.Black, 10, 120)
    redPen.DashStyle = Drawing2D.DashStyle.DashDot
    e.Graphics.DrawLine(redPen, 190, 140, 400, 140)

    e.Graphics.DrawString("DashDotDot", font, Brushes.Black, 10, 160)
    redPen.DashStyle = Drawing2D.DashStyle.DashDotDot
    e.Graphics.DrawLine(redPen, 190, 180, 400, 180)

    e.Graphics.DrawString("Custom", font, Brushes.Black, 10, 200)
    redPen.DashStyle = Drawing2D.DashStyle.Custom
    redPen.DashPattern = {1.0F, 1.0F, 8.0F, 1.0F} 'カスタムパターン。線・隙間・線・隙間・・・の順で長さを指定する。
    e.Graphics.DrawLine(redPen, 190, 220, 400, 220)

End Sub

実行結果

各種点線・破線

 

 

二重線を描画する

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim point1 As New Point(10, 10)
    Dim point2 As New Point(210, 210)

    Dim pen As New Pen(Color.Red, 8) '太さ8の赤いペン

    '線の区切りを定義する。各区間は順に描画区間、空白区間、描画区間、空白区間・・・となる。
    'この例では第1区間(描画)は0~33%, 第2区間(空白)は33~66%、第3区間(描画)は66%~100%となる。
    '結果として中央に空白のある二重線となる。
    pen.CompoundArray = {0, 0.33, 0.67, 1.0}

    e.Graphics.DrawLine(pen, point1, point2)

End Sub

実行結果

二重線

 

 

三重線を描画する

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim point1 As New Point(10, 10)
    Dim point2 As New Point(210, 210)

    Dim pen As New Pen(Color.Red, 8) '太さ8の赤いペン

    '線の区切りを定義する。各区間は順に描画区間、空白区間、描画区間、空白区間・・・となる。
    'この例では第1区間(描画)は0~20%, 第2区間(空白)は20~40%、第3区間(描画)は40%~60%・・・となる。
    '結果として描画区間が3つある三重線になる。
    pen.CompoundArray = {0, 0.2, 0.4, 0.6, 0.8, 1}

    e.Graphics.DrawLine(pen, point1, point2)

End Sub

実行結果

三重線

 

 

カスタムな色の線を描画する

赤42 + 緑210 + 青240 の割合で合成した色で直線を描画します。赤・緑・青の各色は0~255の範囲で指定できます。

VB.NET2002対応 VB.NET2003対応 VB2005対応 VB2008対応 VB2010対応 VB2012対応 VB2013対応 VB2015対応 VB2017対応 VB2019対応

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim point1 As New Point(10, 20)
    Dim point2 As New Point(200, 150)

    Dim pen As New Pen(Color.FromArgb(42, 210, 240), 8) '太さ8のカスタムな色のペン

    e.Graphics.DrawLine(pen, point1, point2)
End Sub

 


VB6対応 VB6では Line を使用します。