0

VB.Net のペイント イベントを実験しています。この実験では、(入力したパラメータに応じて) 水平または垂直の繰り返しラインを作成し、対応する終点 x と y に到達するまでループします。

このようなもの:

ここに画像の説明を入力

ここに画像の説明を入力

私が達成しようとしているのは、x と y の開始点x と y の終了点を指定すると、関数は、指定された開始点から開始し、指定された終了点に到達するまで垂直線または水平線を作成する必要があります。

ペイントイベントを使用して曲線と直線を作成できますが、現在、指定された x と y の始点と終点でループを実行する方法がわかりません。

4

2 に答える 2

2

x/y 座標を繰り返すには、For ループを使用するだけです。次に例を示します。

Public Class Form1

    Private Enum Orientation
        Vertical
        Horizontal
    End Enum

    Protected Overrides Sub OnPaint(e As PaintEventArgs)

        Dim orient As Orientation = Orientation.Vertical
        Dim x As Integer = 100          'X Coord
        Dim y As Integer = 100          'Y Coord
        Dim count As Integer = 10       'Number of Lines to draw
        Dim spacing As Integer = 5      'Spacing between lines in pixels
        Dim length As Integer = 20      'Length of each line in pixels
        Dim thickness As Integer = 3    'Thickness of each line in pixels

        drawLines(x, y, orient, count, spacing, length, thickness, e.Graphics)
    End Sub

    Private Sub drawLines(x As Integer, y As Integer, orient As Orientation, count As Integer, spacing As Integer, length As Integer, thickness As Integer, g As Graphics)

        'Create the Pen in a using block so it will be disposed.  
        'The code uses a red pen, you can use whatever color you want
        Using p As New Pen(Brushes.Red, CSng(thickness))

            'Here we iterate either the x or y coordinate to draw each
            'small segment.
            For i As Integer = 0 To count - 1
                If orient = Orientation.Horizontal Then
                    g.DrawLine(p, x + ((thickness + spacing) * i), y, x + ((thickness + spacing) * i), y + length)
                Else
                    g.DrawLine(p, x, y + ((thickness + spacing) * i), x + length, y + ((thickness + spacing) * i))
                End If
            Next

        End Using

    End Sub
End Class
于 2016-06-20T14:27:45.017 に答える
0

次のようなことを試しましたか:

For x = xstart to xend Step Spacing

Next

どこ:

  • xstart = 開始点
  • xend = エンドポイント
  • 間隔 = 行間の距離
于 2016-06-20T14:17:57.550 に答える