我有代码(下面)允许我根据鼠标移动在imBoat的中点周围旋转4个元素(imRotationOverlay,imBoat,imRotationPanel,imRotationPanel2)。这一直有效,直到它达到246和304之间的角度。我想知道我的代码中的问题在于不允许在这些角度进行旋转。
Dim MousePoint As Point
Private Sub RotationMouseDown() Handles imRotationPanel.MouseDown, imRotationPanel2.MouseDown
MousePoint = Mouse.GetPosition(Application.Current.MainWindow)
End Sub
Sub RotationMouseMove(sender As Image, e As MouseEventArgs) Handles imRotationPanel.MouseMove, imRotationPanel2.MouseMove
If e.LeftButton = MouseButtonState.Pressed Then
Dim midPointOfControl As Point
midPointOfControl.X = imBoat.Margin.Left + (0.5 * imBoat.Width)
midPointOfControl.Y = imBoat.Margin.Top + (0.5 * imBoat.Width)
Dim opposite, adjacent, hypotenuse As Integer
opposite = midPointOfControl.Y - MousePoint.Y
adjacent = midPointOfControl.X - MousePoint.X
hypotenuse = Math.Sqrt(opposite ^ 2 + adjacent ^ 2)
Dim angle As Integer = Math.Acos(adjacent / hypotenuse)
If imRotationPanel.IsMouseOver = True Then
angle = -angle
End If
angle = angle + GetAngle(imBoat)
RotateAllBy(angle)
MousePoint = Mouse.GetPosition(Me)
End If '58 degrees of error... (246-304[-56] where angle [first declaration] returns as 0)
End Sub
Sub RotateAllBy(ByVal angle As Integer)
Rotate(imBoat, angle, imBoat.Width / 2, imBoat.Height / 2)
Rotate(imRotationOverlay, angle, 0.5, 0.5)
Rotate(imRotationPanel2, angle, 0, (imRotationOverlay.Height / 2) - 4)
Rotate(imRotationPanel, angle, imRotationPanel.Width, (imRotationOverlay.Height / 2) - 4)
End Sub
Private Sub Rotate(sender As Object, ByVal rotationAmount As Integer, ByVal centerX As Integer, ByVal centerY As Integer)
Dim rotateTransform As New RotateTransform(rotationAmount)
rotateTransform.CenterX = centerX
rotateTransform.CenterY = centerY
sender.RenderTransform = rotateTransform
End Sub
Private Function GetAngle(sender As Object) As Integer
Dim rotateTransform As New RotateTransform
rotateTransform = sender.RenderTransform
Return rotateTransform.Angle
End Function
全部谢谢
编辑:如果控件只指向鼠标,可以改进此代码吗?如果是这样,我将如何编码呢?答案 0 :(得分:0)
答案 1 :(得分:0)
有时候更简单更好
Private Sub RotationMouseMove(sender As Image, e As MouseEventArgs) Handles imRotationPanel.MouseMove, imRotationPanel2.MouseMove
If e.LeftButton = MouseButtonState.Pressed Then
Dim angle As Integer = 1
If imRotationPanel.IsMouseOver = True Then
angle = -angle
End If
angle = angle + GetAngle(imBoat)
RotateAllBy(angle)
MousePoint = Mouse.GetPosition(Me)
End If
End Sub
修改强>
出了什么问题:在原始解决方案中定义的角度给出的值太小,无法在246-304部分中增加它。
我做了不同的事情:使角度成为设定值,从而降低了原始代码的复杂性
为什么这样做:角度一直是恒定的,因此消除了问题