如何将线条形状旋转 90 度?
How to rotate a line shape 90 Degrees?
我要创建一个包含 60 个线形的表单(使用 Visual Studios PowerPack)。我希望用户能够使用键盘上的左右按钮将形状旋转 90 度。
最好的方法是什么?我尝试过其他方法,但这相当于 1000 行代码,我仍在学习,我想知道最佳实践。
非常感谢!
我暂时假设您已经编写了一次处理几何的部分,并且正在询问如何重新使用代码,而无需将其复制 60 行。这很重要,因为从问题中不能 100% 清楚您是围绕中点还是围绕起点旋转,因为 LineShape 类型确实区分了起点和终点。没有这些信息,我无法为您编写几何代码。
第一部分还不错。我们只是设置了一些可以处理旋转任何线的方法:
'Note that rotating a line 90 degrees around it's midpoint
' will give the same result whether you go clockwise or counterclockwise,
' but I figure you'll want to adapt this for other shapes later, or that
' you're rotating around the line's starting point
Private Sub RotateClockwise(ByVal line As LineShape)
'Code to rotate the passed line clockwise here
Dim x1 As Integer = line.X1
Dim y1 As Integer = line.Y1
Dim x2 As Integer = line.X2
Dim y2 As Integer = line.Y2
End Sub
Private Sub RotateCounterclockwise(ByVal line As LineShape)
'Code to rotate the passed line counter-clockwise here
End Sub
Private Sub LineShape_KeyDown(Byval sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
'Which line?
Dim line As LineShape = TryCast(sender, LineShape)
If line Is Nothing Then Exit Sub
'Left key?
If e.KeyCode = Keys.Left Then RotateCounterclockwise(line)
'Right key?
If e.KeyCode = Keys.Right Then RotateClockwise(line)
End Sub
这就是它变得棘手的地方。请注意,上面的事件处理程序缺少 Handles
关键字。我们想将 LineShape 控件的 all 的 KeyDown
事件处理程序连接到这个方法。这将有点重复,因为这意味着您的表单上的每一行都需要额外一行代码,但这比需要为 all 行编写上述代码要好:
Dim Lines As New List(Of LineShape)()
Lines.Add(LineShape1)
Lines.Add(LineShape2)
'...
Lines.Add(LineShape60)
For Each Line As LineShape In Lines
AddHandler Line.KeyDown, AddressOf LineShape_KeyDown
Next
该代码在 InitializeComponent()
方法之后进入表单的构造函数。
如果 LineShape 类型是真正的控件 (For EAch Line In Me.Controls.OfType(Of LineShape)()
),我还能做得更好,但文档显示这实际上是 Component
,而不是 Control
。
或者,您可以子class LineShape
并将 "rotatability" 构建到您的新 class 中:
Imports Microsoft.VisualBasic.PowerPacks
Public Class MySmartLine
Inherits LineShape
Private Sub RotateClockwise()
'Code to rotate clockwise here
Me.X1 = ...
Me.X2 = ...
End Sub
Private Sub RotateAntiClockwise()
'Code to rotate anti clockwise here
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
If e.KeyCode = Keys.Left Then
RotateAntiClockwise()
End If
If e.KeyCode = Keys.Right Then
RotateClockwise()
End If
End Sub
End Class
构建项目后,自定义 MySmartLine
组件将出现在您的工具箱中,您可以使用它代替 LineShape
。
我要创建一个包含 60 个线形的表单(使用 Visual Studios PowerPack)。我希望用户能够使用键盘上的左右按钮将形状旋转 90 度。
最好的方法是什么?我尝试过其他方法,但这相当于 1000 行代码,我仍在学习,我想知道最佳实践。
非常感谢!
我暂时假设您已经编写了一次处理几何的部分,并且正在询问如何重新使用代码,而无需将其复制 60 行。这很重要,因为从问题中不能 100% 清楚您是围绕中点还是围绕起点旋转,因为 LineShape 类型确实区分了起点和终点。没有这些信息,我无法为您编写几何代码。
第一部分还不错。我们只是设置了一些可以处理旋转任何线的方法:
'Note that rotating a line 90 degrees around it's midpoint
' will give the same result whether you go clockwise or counterclockwise,
' but I figure you'll want to adapt this for other shapes later, or that
' you're rotating around the line's starting point
Private Sub RotateClockwise(ByVal line As LineShape)
'Code to rotate the passed line clockwise here
Dim x1 As Integer = line.X1
Dim y1 As Integer = line.Y1
Dim x2 As Integer = line.X2
Dim y2 As Integer = line.Y2
End Sub
Private Sub RotateCounterclockwise(ByVal line As LineShape)
'Code to rotate the passed line counter-clockwise here
End Sub
Private Sub LineShape_KeyDown(Byval sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
'Which line?
Dim line As LineShape = TryCast(sender, LineShape)
If line Is Nothing Then Exit Sub
'Left key?
If e.KeyCode = Keys.Left Then RotateCounterclockwise(line)
'Right key?
If e.KeyCode = Keys.Right Then RotateClockwise(line)
End Sub
这就是它变得棘手的地方。请注意,上面的事件处理程序缺少 Handles
关键字。我们想将 LineShape 控件的 all 的 KeyDown
事件处理程序连接到这个方法。这将有点重复,因为这意味着您的表单上的每一行都需要额外一行代码,但这比需要为 all 行编写上述代码要好:
Dim Lines As New List(Of LineShape)()
Lines.Add(LineShape1)
Lines.Add(LineShape2)
'...
Lines.Add(LineShape60)
For Each Line As LineShape In Lines
AddHandler Line.KeyDown, AddressOf LineShape_KeyDown
Next
该代码在 InitializeComponent()
方法之后进入表单的构造函数。
如果 LineShape 类型是真正的控件 (For EAch Line In Me.Controls.OfType(Of LineShape)()
),我还能做得更好,但文档显示这实际上是 Component
,而不是 Control
。
或者,您可以子class LineShape
并将 "rotatability" 构建到您的新 class 中:
Imports Microsoft.VisualBasic.PowerPacks
Public Class MySmartLine
Inherits LineShape
Private Sub RotateClockwise()
'Code to rotate clockwise here
Me.X1 = ...
Me.X2 = ...
End Sub
Private Sub RotateAntiClockwise()
'Code to rotate anti clockwise here
End Sub
Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)
MyBase.OnKeyDown(e)
If e.KeyCode = Keys.Left Then
RotateAntiClockwise()
End If
If e.KeyCode = Keys.Right Then
RotateClockwise()
End If
End Sub
End Class
构建项目后,自定义 MySmartLine
组件将出现在您的工具箱中,您可以使用它代替 LineShape
。