我有代碼(如下),允許我根據鼠標移動在imBoat的中點周圍旋轉4個元素(imRotationOverlay,imBoat,imRotationPanel,imRotationPanel2)。這個工作,直到它達到246和304之間的角度。我想知道我的代碼中的問題是不允許在這些角度發生旋轉。WPF:使用點周圍的鼠標移動旋轉控件
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
感謝所有
編輯:如果控制只是對鼠標指向難道這代碼加以改進?如果是這樣,我將如何編碼?
請考慮包括一些關於您的答案的信息,而不是簡單地發佈代碼。我們嘗試提供的不僅僅是「修復」,而是幫助人們學習。你應該解釋原始代碼中的錯誤,你做了什麼不同,以及爲什麼你的改變起作用。 – 2014-10-09 20:14:38
對不起,我現在編輯它 – RedLaser 2014-10-09 21:48:33