How to rotate the MapControl display


Summary
This topic describes rotating the MapControl display and using mouse pointer icons to identify the rotation redraw phase. The OnMouseDown and OnMouseMove events are used to rotate the screen display.

Rotating the MapControl display

Do the following steps to rotate the MapControl display:
  1. In the form's Load event, if required, load a rotate icon. Set the MouseIcon property is to this custom icon using the IPictureDisp.LoadResFile method.
  2. In the MouseDown event, use the IScreenDisplay.RotateStart method to rotate around the center of the display based on the current mouse location. Set the MousePointer property is to esriPointerCustom and the MouseIcon will be used.
  3. In the MouseMove event, use IScreenDisplay.RotateMoveTo method to continue the rotation and RotateTimer to redraw the display.
  4. Use the IScreenDisplay.RotateStop method to determine the angle of rotation. In the OnMouseUp event, set the MapControl's rotation property to this angle and refresh the display to reflect the change.
  5. In the OnBeforeScreenDraw event, set the MousePointer to esriPointerHourglass. This will cause the icon to display while the map is refreshing.

    See the following code example:
[C#]
private void Form1_Load(object sender, System.EventArgs e)
{
    ... mapControl = (IMapControl3)axMapControl1.Object;
    //Load the icon and convert it to an IPictureDisp.
    Icon icon = new Icon(GetType().Assembly.GetManifestResourceStream(GetType(), 
        "Rotate.ico"));
    mapControl.MouseIcon = (stdole.IPictureDisp)
        ESRI.ArcGIS.ADF.COMSupport.OLE.GetIPictureDispFromIcon(icon);
}

private void axMapControl1_OnMouseDown(object sender,
    ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
    //If it is a left button.
    if (e.button == 1)
    {
        //User is not rotating.
        m_bRotating = false;
        //Zoom in
        mapControl.Extent = mapControl.TrackRectangle();
    }
    //If it is a right mouse button.
    else if (e.button == 2)
    {
        //User is rotating.
        m_bRotating = true;
        //Get the IPoint interface.
        IPoint point = new PointClass();
        //Set the coordinates of the current mouse location.
        point.PutCoords(e.mapX, e.mapY);
        //Set the coordinates of the center of the current extent.
        m_Point.X = mapControl.Extent.XMin + (mapControl.Extent.Width / 2);
        m_Point.Y = mapControl.Extent.YMin + (mapControl.Extent.Height / 2);
        //Start rotating the display.
        mapControl.ActiveView.ScreenDisplay.RotateStart(point, m_Point);
        //Set the mouse pointer to use the mouse icon.
        mapControl.MousePointer = esriControlsMousePointer.esriPointerCustom;
    }

    private void axMapControl1_OnMouseMove(object sender,
        ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
    {
        if (m_bRotating == false)
            return ;
        //Get the IPoint interface.
        IPoint point = new PointClass();
        //Set the coordinates of the current mouse location.
        point.PutCoords(e.mapX, e.mapY);
        //Rotate the display based upon the current mouse location.
        mapControl.ActiveView.ScreenDisplay.RotateMoveTo(point);
        //Draw the rotated display.
        mapControl.ActiveView.ScreenDisplay.RotateTimer();
    }

    private void axMapControl1_OnMouseUp(object sender,
        ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
    {
        if (m_bRotating == false)
            return ;
        m_bRotating = false;
        //Get the rotation angle.
        double dRotationAngle = mapControl.ActiveView.ScreenDisplay.RotateStop();
        //Rotate the MapControl's display.
        mapControl.Rotation = dRotationAngle;
        //Refresh the display.
        mapControl.Refresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography,
            Type.Missing, Type.Missing);
    }

    private void axMapControl1_OnAfterScreenDraw(object sender,
        ESRI.ArcGIS.Controls.IMapControlEvents2_OnAfterScreenDrawEvent e)
    {
        if (m_bRotating == false)
        {
            mapControl.MousePointer = esriControlsMousePointer.esriPointerDefault;
        }
    }

    private void axMapControl1_OnBeforeScreenDraw(object sender,
        ESRI.ArcGIS.Controls.IMapControlEvents2_OnBeforeScreenDrawEvent e)
    {
        if (m_bRotating == false)
        {
            mapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass;
        }
    }
[VB.NET]
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
    ...
    m_pMapControl = AxMapControl1.Object
    'Load the icon and convert it to an IPictureDisp.
    Dim pIcon As Icon
    pIcon = New Icon(GetType(RotateDisplay).Assembly.GetManifestResourceStream(GetType(RotateDisplay), "Rotate.ico"))
    m_pMapControl.MouseIcon = ESRI.ArcGIS.ADF.COMSupport.OLE.GetIPictureDispFromIcon(pIcon)
    'Get the IPoint interface.
    m_pPoint = New Point
End Sub

Private Sub AxMapControl1_OnMouseDown(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent) Handles AxMapControl1.OnMouseDown
    'If it is a left button.
    Dim pPoint As ESRI.ArcGIS.Geometry.IPoint
    If e.button = 1 Then
        'User is not rotating.
        m_bRotating = False
        'Zoom in.
        m_pMapControl.Extent = m_pMapControl.TrackRectangle
        'If it is a right mouse button.
    ElseIf e.button = 2 Then
        'User is rotating.
        m_bRotating = True
        'Get the IPoint interface.
        pPoint = New PointClass
        'Set the coordinates of the current mouse location.
        pPoint.PutCoords(e.mapX, e.mapY)
        'Set the coordinates of the center of the current extent.
        m_pPoint.X = m_pMapControl.Extent.XMin + (m_pMapControl.Extent.Width / 2)
        m_pPoint.Y = m_pMapControl.Extent.YMin + (m_pMapControl.Extent.Height / 2)
        'Start rotating the display.
        m_pMapControl.ActiveView.ScreenDisplay.RotateStart(pPoint, m_pPoint)
        'Set the mouse pointer to use the mouse icon.
        m_pMapControl.MousePointer = esriControlsMousePointer.esriPointerCustom
    End If
End Sub

Private Sub AxMapControl1_OnMouseMove(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent) Handles AxMapControl1.OnMouseMove
    If m_bRotating = False Then Exit Sub
    'Get the IPoint interface.
    Dim pPoint As IPoint
    pPoint = New PointClass
    'Set the coordinates of the current mouse location.
    pPoint.PutCoords(e.mapX, e.mapY)
    'Rotate the display based upon the current mouse location.
    m_pMapControl.ActiveView.ScreenDisplay.RotateMoveTo(pPoint)
    'Draw the rotated display.
    m_pMapControl.ActiveView.ScreenDisplay.RotateTimer()
End Sub

Private Sub AxMapControl1_OnMouseUp(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent) Handles AxMapControl1.OnMouseUp
    If m_bRotating = False Then Exit Sub
    m_bRotating = False
    'Get the rotation angle.
    Dim dRotationAngle As Double
    dRotationAngle = m_pMapControl.ActiveView.ScreenDisplay.RotateStop
    'Rotate the MapControl's display.
    m_pMapControl.Rotation = dRotationAngle
    'Refresh the display.
    m_pMapControl.Refresh(ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography)
End Sub

Private Sub AxMapControl1_OnAfterScreenDraw(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.IMapControlEvents2_OnAfterScreenDrawEvent) Handles AxMapControl1.OnAfterScreenDraw
    If m_bRotating = False Then
        m_pMapControl.MousePointer = esriControlsMousePointer.esriPointerDefault
    End If
End Sub

Private Sub AxMapControl1_OnBeforeScreenDraw(ByVal sender As Object, ByVal e As ESRI.ArcGIS.Controls.IMapControlEvents2_OnBeforeScreenDrawEvent) Handles AxMapControl1.OnBeforeScreenDraw
    If m_bRotating = False Then
        m_pMapControl.MousePointer = esriControlsMousePointer.esriPointerHourglass
    End If
End Sub


See Also:

MapControl class
IMapControl2 interface




Additional Requirements
  • A rotate mouse pointer icon.

Development licensing Deployment licensing
Engine Developer Kit Engine
ArcGIS for Desktop Basic
ArcGIS for Desktop Standard
ArcGIS for Desktop Advanced