ArcObjects Library Reference  

AnimatedZoomOutTool

About the Dynamic display animated zoom Sample

[C#]

AnimatedZoomOutTool.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using System.Windows.Forms;

namespace DynamicDisplayAnimatedZoom
{
  /// <summary>
  /// Summary description for AnimatedZoomOutTool.
  /// </summary>
  [Guid("64b7e531-5e4f-4d48-83ef-2b07668509ba")]
  [ClassInterface(ClassInterfaceType.None)]
  [ProgId("DynamicDisplayAnimatedZoom.AnimatedZoomOutTool")]
  public sealed class AnimatedZoomOutTool : BaseTool
  {
    #region COM Registration Function(s)
    [ComRegisterFunction()]
    [ComVisible(false)]
    static void RegisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryRegistration(registerType);
    }

    [ComUnregisterFunction()]
    [ComVisible(false)]
    static void UnregisterFunction(Type registerType)
    {
      // Required for ArcGIS Component Category Registrar support
      ArcGISCategoryUnregistration(registerType);
    }

    #region ArcGIS Component Category Registrar generated code
    /// <summary>
    /// Required method for ArcGIS Component Category registration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryRegistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Register(regKey);
      ControlsCommands.Register(regKey);
    }
    /// <summary>
    /// Required method for ArcGIS Component Category unregistration -
    /// Do not modify the contents of this method with the code editor.
    /// </summary>
    private static void ArcGISCategoryUnregistration(Type registerType)
    {
      string regKey = string.Format("HKEY_CLASSES_ROOT\\CLSID\\{{{0}}}", registerType.GUID);
      MxCommands.Unregister(regKey);
      ControlsCommands.Unregister(regKey);
    }

    #endregion
    #endregion

    #region class members
    private IHookHelper m_hookHelper = null;

    private bool m_bIsAnimating = false;
    private bool m_bZoomIn = false;
    private double m_dStepCount = 0;
    private int m_nTotalSteps = 0;

    private IPoint m_Center = new PointClass();

    private WKSEnvelope m_wksStep = new WKSEnvelope();

    private IDynamicMapEvents_Event m_dynamicMapEvents = null;

    private const double c_dMinimumDelta = 0.01;
    private const double c_dSmoothFactor = 200000.0;
    private const double c_dMinimumSmoothZoom = 0.1;
    #endregion

    public AnimatedZoomOutTool()
    {
      base.m_category = ".NET Samples";
      base.m_caption = "Animated Zoom out";
      base.m_message = "Zoom out with animation";
      base.m_toolTip = "Animated Zoom out";
      base.m_name = "DynamicDisplayAnimatedZoom_AnimatedZoomOutTool";
      try
      {
        string bitmapResourceName = GetType().Name + ".bmp";
        base.m_bitmap = new Bitmap(GetType(), bitmapResourceName);
        base.m_cursor = new System.Windows.Forms.Cursor(GetType(), GetType().Name + ".cur");
      }
      catch (Exception ex)
      {
        System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap");
      }
    }

    #region Overriden Class Methods

    /// <summary>
    /// Occurs when this tool is created
    /// </summary>
    /// <param name="hook">Instance of the application</param>
    public override void OnCreate(object hook)
    {
      if (null == hook)
        return;

      try
      {
        m_hookHelper = new HookHelperClass();
        m_hookHelper.Hook = hook;
        if (null == m_hookHelper.ActiveView)
          m_hookHelper = null;
      }
      catch
      {
        m_hookHelper = null;
      }
    }

    /// <summary>
    /// The enabled state of this command, determines whether the command is usable.
    /// </summary>
    public override bool Enabled
    {
      get
      {
        if (null == m_hookHelper)
          return false;

        IDynamicMap dynamicMap = m_hookHelper.FocusMap as IDynamicMap;
        bool bIsDynamicMapEnabled = dynamicMap.DynamicMapEnabled;
        if (false == bIsDynamicMapEnabled)
        {
          m_bIsAnimating = false;
          m_dStepCount = 0;
          m_nTotalSteps = 0;
          m_dynamicMapEvents = null;
        }
        return bIsDynamicMapEnabled;
      }
    }

    /// <summary>
    /// Occurs when this tool is clicked
    /// </summary>
    public override void OnClick()
    {
      IDynamicMap dynamicMap = m_hookHelper.FocusMap as IDynamicMap;
      if (false == dynamicMap.DynamicMapEnabled)
        return;

      m_dynamicMapEvents = null;
      m_dynamicMapEvents = m_hookHelper.FocusMap as IDynamicMapEvents_Event;
      m_dynamicMapEvents.DynamicMapStarted += new IDynamicMapEvents_DynamicMapStartedEventHandler(DynamicMapEvents_DynamicMapStarted);

      m_bIsAnimating = false;
      m_dStepCount = 0;
      m_nTotalSteps = 0;
    }

    public override void OnMouseDown(int Button, int Shift, int X, int Y)
    {
      // Zoom on the focus map based on user drawn rectangle
      m_bZoomIn = Shift == 1;

      IActiveView activeView = m_hookHelper.FocusMap as IActiveView;
      IRubberBand rubberBand = new RubberEnvelopeClass();
      // This method intercepts the Mouse events from here
      IEnvelope zoomBounds = rubberBand.TrackNew(activeView.ScreenDisplay, null) as IEnvelope;
      if (null == zoomBounds)
        return;

      WKSEnvelope wksZoomBounds;
      zoomBounds.QueryWKSCoords(out wksZoomBounds);

      IEnvelope fittedBounds = activeView.ScreenDisplay.DisplayTransformation.FittedBounds;
      WKSEnvelope wksFittedBounds;
      fittedBounds.QueryWKSCoords(out wksFittedBounds);

      if (false == m_bZoomIn)
      {
        double dXScale = fittedBounds.Width  * fittedBounds.Width  / zoomBounds.Width;
        double dYScale = fittedBounds.Height * fittedBounds.Height / zoomBounds.Height;

        wksZoomBounds.XMin = fittedBounds.XMin - dXScale;
        wksZoomBounds.YMin = fittedBounds.YMin - dYScale;
        wksZoomBounds.XMax = fittedBounds.XMax + dXScale;
        wksZoomBounds.YMax = fittedBounds.YMax + dYScale;
      }

      m_wksStep.XMax = 1;
      m_wksStep.YMax = 1;
      m_wksStep.XMin = 1;
      m_wksStep.YMin = 1;
      m_nTotalSteps = 0;

      // Calculate how fast the zoom will go by changing the step size
      while ((System.Math.Abs(m_wksStep.XMax) > c_dMinimumDelta) ||
             (System.Math.Abs(m_wksStep.YMax) > c_dMinimumDelta) ||
             (System.Math.Abs(m_wksStep.XMin) > c_dMinimumDelta) ||
             (System.Math.Abs(m_wksStep.YMin) > c_dMinimumDelta))
      {
        m_nTotalSteps++;

        // calculate the step size
        // step size is the difference between the zoom bounds and the fitted bounds
        m_wksStep.XMin = (wksZoomBounds.XMin - wksFittedBounds.XMin) / m_nTotalSteps;
        m_wksStep.YMin = (wksZoomBounds.YMin - wksFittedBounds.YMin) / m_nTotalSteps;
        m_wksStep.XMax = (wksZoomBounds.XMax - wksFittedBounds.XMax) / m_nTotalSteps;
        m_wksStep.YMax = (wksZoomBounds.YMax - wksFittedBounds.YMax) / m_nTotalSteps;
      }

      m_bIsAnimating = true;
      m_dStepCount = 0;
    }

    public override bool Deactivate()
    {
      m_bIsAnimating = false;
      m_dStepCount = 0;
      m_nTotalSteps = 0;

      if (null == m_hookHelper)
        return false;

      IDynamicMap dynamicMap = m_hookHelper.FocusMap as IDynamicMap;
      if (false == dynamicMap.DynamicMapEnabled)
        return true;

      m_dynamicMapEvents = m_hookHelper.FocusMap as IDynamicMapEvents_Event;
      m_dynamicMapEvents.DynamicMapStarted -= new IDynamicMapEvents_DynamicMapStartedEventHandler(DynamicMapEvents_DynamicMapStarted);

      return true;
    }
    #endregion

    #region Dynamic Map Events
    void DynamicMapEvents_DynamicMapStarted(IDisplay Display, IDynamicDisplay dynamicDisplay)
    {
      if (false == m_bIsAnimating)
      {
        m_dStepCount = 0;
        m_nTotalSteps = 0;
        return;
      }

      if (m_dStepCount >= m_nTotalSteps)
      {
        m_bIsAnimating = false;
        m_dStepCount = 0;
        m_nTotalSteps = 0;
        return;
      }

      // Increase the bounds by the step amount
      IActiveView activeView = m_hookHelper.FocusMap as IActiveView;
      IEnvelope newVisibleBounds = activeView.ScreenDisplay.DisplayTransformation.FittedBounds;

      // Smooth the zooming.  Faster at higher scales, slower at lower
      double dSmoothZooom = activeView.FocusMap.MapScale / c_dSmoothFactor;
      if (dSmoothZooom < c_dMinimumSmoothZoom)
        dSmoothZooom = c_dMinimumSmoothZoom;

      newVisibleBounds.XMin = newVisibleBounds.XMin + (m_wksStep.XMin * dSmoothZooom);
      newVisibleBounds.YMin = newVisibleBounds.YMin + (m_wksStep.YMin * dSmoothZooom);
      newVisibleBounds.XMax = newVisibleBounds.XMax + (m_wksStep.XMax * dSmoothZooom);
      newVisibleBounds.YMax = newVisibleBounds.YMax + (m_wksStep.YMax * dSmoothZooom);

      activeView.ScreenDisplay.DisplayTransformation.VisibleBounds = newVisibleBounds;

      m_dStepCount = m_dStepCount + dSmoothZooom;
    }
    #endregion
  }
}

[Visual Basic .NET]

AnimatedZoomOutTool.vb

Imports Microsoft.VisualBasic
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.ADF.BaseClasses
Imports ESRI.ArcGIS.ADF.CATIDs
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Controls
Imports System.Windows.Forms

  ''' <summary>
  ''' Summary description for AnimatedZoomOutTool.
  ''' </summary>
  <Guid("64b7e531-5e4f-4d48-83ef-2b07668509ba"), ClassInterface(ClassInterfaceType.None), ProgId("AnimatedZoomOutTool")> _
  Public NotInheritable Class AnimatedZoomOutTool : Inherits BaseTool
	#Region "COM Registration Function(s)"
	<ComRegisterFunction(), ComVisible(False)> _
	Private Shared Sub RegisterFunction(ByVal registerType As Type)
	  ' Required for ArcGIS Component Category Registrar support
	  ArcGISCategoryRegistration(registerType)
	End Sub

	<ComUnregisterFunction(), ComVisible(False)> _
	Private Shared Sub UnregisterFunction(ByVal registerType As Type)
	  ' Required for ArcGIS Component Category Registrar support
	  ArcGISCategoryUnregistration(registerType)
	End Sub

	#Region "ArcGIS Component Category Registrar generated code"
	''' <summary>
	''' Required method for ArcGIS Component Category registration -
	''' Do not modify the contents of this method with the code editor.
	''' </summary>
	Private Shared Sub ArcGISCategoryRegistration(ByVal registerType As Type)
	  Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
	  MxCommands.Register(regKey)
	  ControlsCommands.Register(regKey)
	End Sub
	''' <summary>
	''' Required method for ArcGIS Component Category unregistration -
	''' Do not modify the contents of this method with the code editor.
	''' </summary>
	Private Shared Sub ArcGISCategoryUnregistration(ByVal registerType As Type)
	  Dim regKey As String = String.Format("HKEY_CLASSES_ROOT\CLSID\{{{0}}}", registerType.GUID)
	  MxCommands.Unregister(regKey)
	  ControlsCommands.Unregister(regKey)
	End Sub

	#End Region
	#End Region

	#Region "class members"
	Private m_hookHelper As IHookHelper = Nothing

	Private m_bIsAnimating As Boolean = False
	Private m_bZoomIn As Boolean = False
	Private m_dStepCount As Double = 0
	Private m_nTotalSteps As Integer = 0

	Private m_Center As IPoint = New PointClass()

	Private m_wksStep As WKSEnvelope = New WKSEnvelope()

	Private m_dynamicMapEvents As IDynamicMapEvents_Event = Nothing

	Private Const c_dMinimumDelta As Double = 0.01
	Private Const c_dSmoothFactor As Double = 200000.0
	Private Const c_dMinimumSmoothZoom As Double = 0.1
	#End Region

	Public Sub New()
	  MyBase.m_category = ".NET Samples"
	  MyBase.m_caption = "Animated Zoom out"
	  MyBase.m_message = "Zoom out with animation"
	  MyBase.m_toolTip = "Animated Zoom out"
	  MyBase.m_name = "AnimatedZoomOutTool"
	  Try
			Dim bitmapResourceName As String = Me.GetType().Name & ".bmp"
			MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
			MyBase.m_cursor = New System.Windows.Forms.Cursor(Me.GetType(), Me.GetType().Name & ".cur")
	  Catch ex As Exception
			System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
	  End Try
	End Sub

#Region "Overridden Class Methods"

    ''' <summary>
    ''' Occurs when this tool is created
    ''' </summary>
    ''' <param name="hook">Instance of the application</param>
    Public Overrides Sub OnCreate(ByVal hook As Object)
        If Nothing Is hook Then
            Return
        End If

        Try
            m_hookHelper = New HookHelperClass()
            m_hookHelper.Hook = hook
            If Nothing Is m_hookHelper.ActiveView Then
                m_hookHelper = Nothing
            End If
        Catch
            m_hookHelper = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' The enabled state of this command, determines whether the command is usable.
    ''' </summary>
    Public Overrides ReadOnly Property Enabled() As Boolean
        Get
            If Nothing Is m_hookHelper Then
                Return False
            End If

            Dim dynamicMap As IDynamicMap = TryCast(m_hookHelper.FocusMap, IDynamicMap)
            Dim bIsDynamicMapEnabled As Boolean = dynamicMap.DynamicMapEnabled
            If False = bIsDynamicMapEnabled Then
                m_bIsAnimating = False
                m_dStepCount = 0
                m_nTotalSteps = 0
                m_dynamicMapEvents = Nothing
            End If
            Return bIsDynamicMapEnabled
        End Get
    End Property

    ''' <summary>
    ''' Occurs when this tool is clicked
    ''' </summary>
    Public Overrides Sub OnClick()
        Dim dynamicMap As IDynamicMap = TryCast(m_hookHelper.FocusMap, IDynamicMap)
        If False = dynamicMap.DynamicMapEnabled Then
            Return
        End If

        m_dynamicMapEvents = Nothing
        m_dynamicMapEvents = TryCast(m_hookHelper.FocusMap, IDynamicMapEvents_Event)
        AddHandler m_dynamicMapEvents.DynamicMapStarted, AddressOf DynamicMapEvents_DynamicMapStarted

        m_bIsAnimating = False
        m_dStepCount = 0
        m_nTotalSteps = 0
    End Sub

    Public Overrides Sub OnMouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Integer, ByVal Y As Integer)
        ' Zoom on the focus map based on user drawn rectangle
        m_bZoomIn = Shift = 1

        Dim activeView As IActiveView = TryCast(m_hookHelper.FocusMap, IActiveView)
        Dim rubberBand As IRubberBand = New RubberEnvelopeClass()
        ' This method intercepts the Mouse events from here
        Dim zoomBounds As IEnvelope = TryCast(rubberBand.TrackNew(activeView.ScreenDisplay, Nothing), IEnvelope)
        If Nothing Is zoomBounds Then
            Return
        End If

        Dim wksZoomBounds As WKSEnvelope
        zoomBounds.QueryWKSCoords(wksZoomBounds)

        Dim fittedBounds As IEnvelope = activeView.ScreenDisplay.DisplayTransformation.FittedBounds
        Dim wksFittedBounds As WKSEnvelope
        fittedBounds.QueryWKSCoords(wksFittedBounds)

        If False = m_bZoomIn Then
            Dim dXScale As Double = fittedBounds.Width * fittedBounds.Width / zoomBounds.Width
            Dim dYScale As Double = fittedBounds.Height * fittedBounds.Height / zoomBounds.Height

            wksZoomBounds.XMin = fittedBounds.XMin - dXScale
            wksZoomBounds.YMin = fittedBounds.YMin - dYScale
            wksZoomBounds.XMax = fittedBounds.XMax + dXScale
            wksZoomBounds.YMax = fittedBounds.YMax + dYScale
        End If

        m_wksStep.XMax = 1
        m_wksStep.YMax = 1
        m_wksStep.XMin = 1
        m_wksStep.YMin = 1
        m_nTotalSteps = 0

        ' Calculate how fast the zoom will go by changing the step size
        Do While (System.Math.Abs(m_wksStep.XMax) > c_dMinimumDelta) OrElse (System.Math.Abs(m_wksStep.YMax) > c_dMinimumDelta) OrElse (System.Math.Abs(m_wksStep.XMin) > c_dMinimumDelta) OrElse (System.Math.Abs(m_wksStep.YMin) > c_dMinimumDelta)
            m_nTotalSteps += 1

            ' calculate the step size
            ' step size is the difference between the zoom bounds and the fitted bounds
            m_wksStep.XMin = (wksZoomBounds.XMin - wksFittedBounds.XMin) / m_nTotalSteps
            m_wksStep.YMin = (wksZoomBounds.YMin - wksFittedBounds.YMin) / m_nTotalSteps
            m_wksStep.XMax = (wksZoomBounds.XMax - wksFittedBounds.XMax) / m_nTotalSteps
            m_wksStep.YMax = (wksZoomBounds.YMax - wksFittedBounds.YMax) / m_nTotalSteps
        Loop

        m_bIsAnimating = True
        m_dStepCount = 0
    End Sub

    Public Overrides Function Deactivate() As Boolean
        m_bIsAnimating = False
        m_dStepCount = 0
        m_nTotalSteps = 0

        If Nothing Is m_hookHelper Then
            Return False
        End If

        Dim dynamicMap As IDynamicMap = TryCast(m_hookHelper.FocusMap, IDynamicMap)
        If False = dynamicMap.DynamicMapEnabled Then
            Return True
        End If

        m_dynamicMapEvents = TryCast(m_hookHelper.FocusMap, IDynamicMapEvents_Event)
        RemoveHandler m_dynamicMapEvents.DynamicMapStarted, AddressOf DynamicMapEvents_DynamicMapStarted

        Return True
    End Function
#End Region

	#Region "Dynamic Map Events"
	Private Sub DynamicMapEvents_DynamicMapStarted(ByVal Display As IDisplay, ByVal dynamicDisplay As IDynamicDisplay)
	  If False = m_bIsAnimating Then
			m_dStepCount = 0
			m_nTotalSteps = 0
			Return
	  End If

	  If m_dStepCount >= m_nTotalSteps Then
			m_bIsAnimating = False
			m_dStepCount = 0
			m_nTotalSteps = 0
			Return
	  End If

	  ' Increase the bounds by the step amount
	  Dim activeView As IActiveView = TryCast(m_hookHelper.FocusMap, IActiveView)
	  Dim newVisibleBounds As IEnvelope = activeView.ScreenDisplay.DisplayTransformation.FittedBounds

	  ' Smooth the zooming.  Faster at higher scales, slower at lower
	  Dim dSmoothZooom As Double = activeView.FocusMap.MapScale / c_dSmoothFactor
	  If dSmoothZooom < c_dMinimumSmoothZoom Then
			dSmoothZooom = c_dMinimumSmoothZoom
	  End If

	  newVisibleBounds.XMin = newVisibleBounds.XMin + (m_wksStep.XMin * dSmoothZooom)
	  newVisibleBounds.YMin = newVisibleBounds.YMin + (m_wksStep.YMin * dSmoothZooom)
	  newVisibleBounds.XMax = newVisibleBounds.XMax + (m_wksStep.XMax * dSmoothZooom)
	  newVisibleBounds.YMax = newVisibleBounds.YMax + (m_wksStep.YMax * dSmoothZooom)

	  activeView.ScreenDisplay.DisplayTransformation.VisibleBounds = newVisibleBounds

	  m_dStepCount = m_dStepCount + dSmoothZooom
	End Sub
	#End Region
  End Class