ArcObjects Library Reference  

RubberBandZoom

About the RubberBand zoom Sample

[C#]

RubberBandZoom.cs

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using System.Windows.Forms;

namespace RubberBandZoom
{
  public class RubberBandZoom : ESRI.ArcGIS.Desktop.AddIns.Tool
  {

    private IRubberBand m_RubberBand = null;
    private IGeometry docGeometry = null;
  
    public RubberBandZoom()
    {
    }

    protected override void OnUpdate()
    {
      Enabled = ArcMap.Application != null;
    }

    protected override void OnActivate()
    {

      //initialize variables.
      docGeometry = null;
      m_RubberBand = null;
      base.OnActivate();
    }


    protected override bool OnContextMenu(int Button, int Shift)
    {
      //returning true for this function overrides the default context menus - we don't want them to come up.
      return true;
    }

    protected override void OnMouseDown(MouseEventArgs Args)//int Button, int Shift, int X, int Y)
    {
      m_RubberBand = new RubberRectangularPolygonClass();


      if (Args.Button.ToString() == "Left")  //left click
      {
        //create a new rubberband polygon.
        docGeometry = m_RubberBand.TrackNew(ArcMap.Document.ActiveView.ScreenDisplay, null);

        //zoom to the selected envelope as long as it is not zero.
        if (!docGeometry.IsEmpty)
        {
          if (docGeometry.Envelope.Height != 0 && docGeometry.Envelope.Width != 0)
          {
            ArcMap.Document.ActiveView.Extent = docGeometry.Envelope;

            //refresh to show changes.
            ArcMap.Document.ActiveView.Refresh();
          }
        }

      }
      else if (Args.Button.ToString() == "Right") //right click
      {
        //zoom out either to previous extent, or to the full extent of the active view.
        if (ArcMap.Document.ActiveView.ExtentStack.CanUndo())
        {
          //if we can, go to a previous zoom extent.
          ArcMap.Document.ActiveView.ExtentStack.Undo();
          ArcMap.Document.ActiveView.Refresh();

        }
        else
        {

          //or if no previous extents exist, go to the full extent of the active view.
          ArcMap.Document.ActiveView.Extent = ArcMap.Document.ActiveView.FullExtent.Envelope;
          ArcMap.Document.ActiveView.Refresh();
        }

      }
    }
  }

}

[Visual Basic .NET]

RubberBandZoom.vb

Imports System.Runtime.InteropServices
Imports System.Drawing
Imports ESRI.ArcGIS.Carto
Imports ESRI.ArcGIS.Display
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geometry
Imports ESRI.ArcGIS.SystemUI

Public Class RubberBandZoom
    Inherits ESRI.ArcGIS.Desktop.AddIns.Tool

    Private m_RubberBand As IRubberBand
    Private docGeometry As IGeometry

    Public Sub New()

    End Sub

    Protected Overrides Sub OnUpdate()
        Enabled = My.ArcMap.Application IsNot Nothing
    End Sub

    Protected Overrides Sub OnActivate()
        docGeometry = Nothing
        m_RubberBand = Nothing
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal Args As MouseEventArgs)
        m_RubberBand = New RubberRectangularPolygon()

        If (Args.Button.ToString = "Left") Then
            'on a left-click, create a new rubber polygon and zoom to its' final extent
            docGeometry = m_RubberBand.TrackNew(My.ArcMap.Document.ActiveView.ScreenDisplay, Nothing)

            'if it is non-zero, zoom to the envelope just captured
            If (docGeometry.IsEmpty = False) Then
                My.ArcMap.Document.ActiveView.Extent = docGeometry.Envelope
            End If


            'and refresh
            My.ArcMap.Document.ActiveView.Refresh()

        ElseIf (Args.Button.ToString = "Right") Then
            'on a right-click, zoom out to the previous extent, or to full extent if no previous extent exists
            If (My.ArcMap.Document.ActiveView.ExtentStack.CanUndo) Then
                'if possible, go back to the previous extent
                My.ArcMap.Document.ActiveView.ExtentStack.Undo()

            Else
                'otherwise, zoom to the active view's full extent
                My.ArcMap.Document.ActiveView.Extent = My.ArcMap.Document.ActiveView.FullExtent.Envelope

            End If

            'and refresh
            My.ArcMap.Document.ActiveView.Refresh()

        End If



    End Sub

    Protected Overrides Function OnContextMenu(ByVal X As Integer, ByVal Y As Integer) As Boolean
        'return true so that the context menu does not come up.
        Return True
    End Function

End Class