我正在尝试 VB.Net 的绘制事件,对于这个实验,我想创建一条重复的水平或垂直(取决于我输入的参数)线并循环直到它遇到相应的端点 x 和 y。
像这样的东西:
我想要实现的是给定x 和 y 起点以及x 和 y 终点,该函数应创建从给定起点开始的垂直或水平线,直到到达给定终点。
我可以使用paintevent创建曲线和直线,但现在我不知道如何在给定的x和y起点和终点执行循环。
我正在尝试 VB.Net 的绘制事件,对于这个实验,我想创建一条重复的水平或垂直(取决于我输入的参数)线并循环直到它遇到相应的端点 x 和 y。
像这样的东西:
我想要实现的是给定x 和 y 起点以及x 和 y 终点,该函数应创建从给定起点开始的垂直或水平线,直到到达给定终点。
我可以使用paintevent创建曲线和直线,但现在我不知道如何在给定的x和y起点和终点执行循环。
您只需要使用 For 循环来迭代 x/y 坐标。这是一个例子:
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
您是否尝试过类似的方法:
For x = xstart to xend Step Spacing
Next
在哪里: