How to rotate the data frame


This sample rotates the data in the focus map. This tool works just like the Rotate Data Frame tool that ships with ArcMap except it does not use operations. This means the rotation cannot be undone.

How to use

  1. Add a UIToolControl to any toolbar.
  2. Paste this code in for the control. Make sure the names match.
  3. Load some data and zoom into a desired location.
  4. Select the UIToolControl.
  5. As you drag an arc on the focus map, the focus map rotates.
  6. Release the mouse to set the final rotation angle.
[VBA]
Option Explicit

Private m_pMxDoc As IMxDocument
Private m_pActiveView As IActiveView
Private m_pScreenDisplay As IScreenDisplay
Private m_bRotating As Boolean

Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
    Dim pEnv As IEnvelope
    Dim pCenterPoint As IPoint
    Dim pTransform As IDisplayTransformation
    
    Set m_pMxDoc = Application.Document
    Set m_pActiveView = m_pMxDoc.FocusMap
    Set m_pScreenDisplay = m_pActiveView.ScreenDisplay
    
    'Rotate around the display's center point
    Set pTransform = m_pScreenDisplay.DisplayTransformation
    Set pEnv = pTransform.FittedBounds
    Set pCenterPoint = New Point
    pCenterPoint.PutCoords ((pEnv.XMax + pEnv.XMin) / 2), ((pEnv.YMax + pEnv.YMin) / 2)
    
    'Start the rotation
    m_pScreenDisplay.RotateStart m_pMxDoc.CurrentLocation, pCenterPoint
    m_bRotating = True
    
End Sub

Private Sub UIToolControl1_MouseMove(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
    If Not button = 1 Then Exit Sub
    If Not m_bRotating Then Exit Sub
    'Update rotation
    m_pScreenDisplay.RotateMoveTo m_pMxDoc.CurrentLocation
    m_pScreenDisplay.RotateTimer
End Sub

Private Sub UIToolControl1_MouseUp(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long)
    Dim dRotationAngle As Double
    If Not button = 1 Then Exit Sub
    'Complete the rotation and refresh the diplay
    m_bRotating = False
    dRotationAngle = m_pScreenDisplay.RotateStop
    m_pScreenDisplay.DisplayTransformation.Rotation = dRotationAngle
    m_pActiveView.Refresh
End Sub