Vb.net 通过给定x和y坐标通过循环创建重复线
我正在VB.Net的paint事件上进行实验,对于这个实验,我想创建一条重复的水平或垂直(取决于我输入的参数)线并循环,直到它满足相应的端点x和y 大概是这样的: 我试图实现的是给定x和y起点和x和y终点函数应创建从给定起点开始直到到达给定终点的垂直或水平线Vb.net 通过给定x和y坐标通过循环创建重复线,vb.net,coordinates,paintevent,Vb.net,Coordinates,Paintevent,我正在VB.Net的paint事件上进行实验,对于这个实验,我想创建一条重复的水平或垂直(取决于我输入的参数)线并循环,直到它满足相应的端点x和y 大概是这样的: 我试图实现的是给定x和y起点和x和y终点函数应创建从给定起点开始直到到达给定终点的垂直或水平线 我可以使用paintevent创建曲线和直线,但现在我不知道如何在给定的x和y起点和终点执行循环。您是否尝试过类似的方法: For x = xstart to xend Step Spacing Next 其中: xstart=您
我可以使用paintevent创建曲线和直线,但现在我不知道如何在给定的x和y起点和终点执行循环。您是否尝试过类似的方法:
For x = xstart to xend Step Spacing
Next
其中:
- xstart=您的起点
- xend=您的终点
- 间距=线之间的距离
For x = xstart to xend Step Spacing
Next
其中:
- xstart=您的起点
- xend=您的终点
- 间距=线之间的距离
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/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
这真是太棒了!我不指望它会这么简单。这真的很棒!我不指望事情会这么简单。