2014-10-09 76 views
0

我有代碼(如下),允許我根據鼠標移動在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 

感謝所有

編輯:如果控制只是對鼠標指向難道這代碼加以改進?如果是這樣,我將如何編碼?

回答

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節最初的解決方案是給太小的值來增加它定義的角度。

我沒有什麼不同:做這樣的設定值降低的原代碼

的複雜性爲什麼這個工作的角度:角度是恆定的輪一路從而排除故障

+0

請考慮包括一些關於您的答案的信息,而不是簡單地發佈代碼。我們嘗試提供的不僅僅是「修復」,而是幫助人們學習。你應該解釋原始代碼中的錯誤,你做了什麼不同,以及爲什麼你的改變起作用。 – 2014-10-09 20:14:38

+0

對不起,我現在編輯它 – RedLaser 2014-10-09 21:48:33

0

請考慮使用RotateTransforms。 轉換內置於WPF中,用於縮放,旋轉和移動。

link

然後可以使用Expression Blend的管理旋轉轉變爲您服務。與所有手動編碼相比。

+0

對不起,我忘了提到Rotate函數使用它。我忘記了一點代碼。編輯代碼 – RedLaser 2014-10-09 16:38:46